Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > CSS > CSS Cascading Style Sheets
CSS Cascading Style Sheets All issues relating to Cascading Style Sheets (CSS).
Welcome to the p2p.wrox.com Forums.

You are currently viewing the CSS Cascading Style Sheets section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old September 11th, 2008, 03:34 PM
Registered User
Join Date: Sep 2008
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default how to get background image url?

Hello everyone,

Is there a way to get the background image url using DOM properties?

I tried this

var x=document.getElementById('navBackground').style.b ackgroundImage;

but an empty alert box shows up.

Thanks in advance!

Old October 7th, 2008, 05:33 AM
Friend of Wrox
Join Date: Mar 2007
Posts: 373
Thanks: 0
Thanked 1 Time in 1 Post

It works only when you set the background image through "style" attribute. I am not that good in CSS and Java script. Some body here can put some light on it.

- Rakesh
Old October 23rd, 2008, 11:06 AM
Registered User
Join Date: Oct 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts

this was actually harder to find than I thought it would be (I needed the same ability).

var x=document.getElementById('navBackground');
var xStyle=x.style;
var xBG = xStyle.backgroundImage;
this should return the full path in the CSS definition, including "url(...)". I typically define background-image:url(...);, background-position:...; background-repeat:...;

In short, I don't know what happens if you use short hand like background: url(...) left top #000;, for example.

Hope this helps you!

Similar Threads
Thread Thread Starter Forum Replies Last Post
Background Image for Different Resolutions Andraw Pro VB 6 0 November 6th, 2008 12:00 PM
Background image in IE7 Samurai CSS Cascading Style Sheets 0 May 21st, 2008 10:46 PM
stretching the background image?? nerssi CSS Cascading Style Sheets 3 February 3rd, 2007 04:55 PM
Background-image:url() interrupt CSS Cascading Style Sheets 2 September 20th, 2006 05:04 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.