Click here to Skip to main content
15,892,737 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to change background image on page load in asp.net mvc
Posted

1 solution

When the page loads, you can trigger the jQuery to change the background-image of your HTML document. It depends on which element you set the background-image, to your body or to your div container because ASP.NET allows you to create a similar layout and then load your website's content into the layout.

Anyway, the code to change the background-image in jQuery is as simple as changing the CSS properties of the elements.

JavaScript
// Make sure you're having jQuery included to your project
$(document).ready(function () {
   // body has been loaded
   $('body').load(function () {
      // Change the background-image
      $('element').css('background-image', 'url(location-of-image.png)');
   });
});


The above code would update background-image upon the body load event. The element should be replaced by the element with its ID that you want to get the new background-image; can be body too.

I didn't do this the ASP.NET MVC way, because that would require registering the scripts or function upon different application based functions and so on. jQuery way is pretty much simple. :)
 
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