Click here to Skip to main content
15,888,286 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to set background image on html(body tag) using javascript. image should be displayed when page is opened.every time page is loaded.
Posted
Comments
jaket-cp 13-Nov-15 10:41am    
Why do you what it to be done in javascript?

css should do the trick and would be the correct way of doing it.
Check out:
https://developer.mozilla.org/en-US/docs/Web/CSS/background-image
manish-gusain8909 14-Nov-15 0:37am    
because by using javascript , i can develop image slider

jQuery:
JavaScript
$(document).ready(function() {
    $("body").css({"background" : "url('image.png')"});
})

Javascript:
JavaScript
document.body.style.backgroundImage = "url('image.png')";
But, better use a CSS:
CSS
body{
    background-image: url('image.png');
}

Both the option will load the image whenever your page load

-KR
 
Share this answer
 
v3
Comments
manish-gusain8909 13-Nov-15 22:32pm    
i want in javascript
Solution 1 answers your question quite nicely, but this may help you in your goal of creating an image slider.

From your comments, you mention you want to make a javascript image slider of some sorts.

Firstly google is your friend so use it wisely.

Performing search using key terms:
javascript image slider [tutorial]

javascript non-jQuery:
https://coderwall.com/p/vsdrug/how-to-create-an-image-slider-with-javascript[^]

Or search the CodeProject articles section
JQuery Image Slider Tutorial for Beginners: JQuery Image Slider Tutorial for Beginners [^]

Here are some additional links which may also help clarify
What is the difference between JavaScript and jQuery? [^]
jQuery .ready() [^]
jQuery css [^]


Or pre-build solutions/plugins which may help:
http://www.learningjquery.com/2015/02/best-jquery-image-slider-plugins[^]
http://www.menucool.com/javascript-image-slider[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900