0
window.onload = function(){
document.body.style.backgroundImage = "url('a.jpg')";
}

a.jpg is in the same forlder.

but

document.body.style.backgroundColor = "blue";

works fine!

whats going wrong?

2
  • your code is working fine. Commented Dec 8, 2013 at 9:35
  • Solved : the image path is not relative to javascript file. It is relative to the html document! strange but it worked! Don't know why javascript is designed this way! Commented Dec 8, 2013 at 9:59

2 Answers 2

1

try

document.body.style.background = 'url("a.jpg")';

this way you apply to the body a background style property. Similar as you would do with a CSS declaration. See http://www.w3schools.com/cssref/css3_pr_background.asp

Or, more similar to your code

document.body.style.backgroundImage = "url(a.jpg)";

without the single quotes. See http://www.w3schools.com/cssref/pr_background-image.asp

If still doesn't work then it is the path to the image that's wrong.

Sign up to request clarification or add additional context in comments.

Comments

0
window.onload = function(){
document.body.style.backgroundImage = "url(a.jpg)";
}

fiddle:Demo

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.