Solved

Dynamically change image SRC

Posted on 2006-10-19
2
438 Views
Last Modified: 2012-05-05
Hello,

How does one dynamically alter the background image based upon the screen resolution? Basically, I have 2 images. If the user is a laptop user, they will always be 1024. If they are a desktop user, they will always be 800. I want to alter tbe <Body Background="somepath.gif"> entry to reflect that when they open the page.

<Script language="javascript">
            function loadBackground ()      {
            if (screen.width<=800) {
                  //alert('800');
                                                //Document.write(Body HTML for Image800.gif)
                  }
                  else  {
                  //alert('greater than');
                                                //Document.write(Body HTML for Image1024.gif)
                  }
            }
</Script>
0
Comment
Question by:SWRO
2 Comments
 
LVL 5

Accepted Solution

by:
aaronchan earned 125 total points
ID: 17771010
Remember to add onload event on your <body>.  Good luck!

<Script language="javascript">
      function loadBackground()     {
            if (screen.width <= 800) {
                  //alert('800');
                  //Document.write(Body HTML for Image800.gif)
                  document.body.background = 'http://i.i.com.com/cnwk.1d/i/hdft/redball.gif';
            }
            else  {
                  //alert('greater than');
                  //Document.write(Body HTML for Image1024.gif)
                  document.body.background = 'http://www.experts-exchange.com/images/10Years.gif';
            }
      }
</Script>
<body onload="loadBackground();">
</body>
0
 

Author Comment

by:SWRO
ID: 17771405
Thanks!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

27 Experts available now in Live!

Get 1:1 Help Now