Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

getting element id from a js file 1

Status
Not open for further replies.

davikokar

Technical User
May 13, 2004
523
IT
hallo,

I have this code into a js file, it is loaded when I open a webpage. The purpose of the code is to show a different background image each day, looping through the available pictures. The problem is that the last line of the code doesn't seem to work and since I'm not an expert of js I don't know why... any idea?

Code:
window.onload = function() {
   var today = new Date();
   var first = new Date(today.getFullYear(),0,1);
   var theDay = Math.round(((today-first)/1000/60/60/24)+.5,0);
   var picNr = 10;
   var picUse;
   picUse = theDay%picNr;

   document.getElementById('Top').style.background='url(../img/'+this.picUse+'.jpg)';
}
 
The main problem of the line, as I see it, is the misunderstood of the scope of this. If picUser is global scope, it would be fine. But it is local to the onload handler, hence, the "this" reference should be taken out.

> document.getElementById('Top').style.background='url(../img/'+[red]this.picUse[/red]+'.jpg)';
[tt]document.getElementById('Top').style.background='url(../img/'+[blue]picUse[/blue]+'.jpg)';[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top