SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Addict
    Join Date
    Mar 2003
    Location
    UK
    Posts
    245
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    2 jscripts on 1 page, both use onload :(

    could someone look at this for me.
    I have two javascripts loading on one page, one for the dropdown menus the other selects a random image for a Div background. Unfortunately both use a onload command to run, so they both wont work together (in IE at any rate).
    Is there any (simple?)changes I can make so they don't conflict?
    This is the Dropdown script
    Code:
    sfHover = function() {
    	var sfEls = document.getElementById("headnav").getElementsByTagName("LI");
    	for (var i=0; i<sfEls.length; i++) {
    		sfEls[i].onmouseover=function() {
    			this.className+=" sfhover";
    		}
    		sfEls[i].onmouseout=function() {
    			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
    		}
    	}
    }
    if (document.all&&document.getElementById) window.onload=sfHover;
    and this is the random background image script
    Code:
    var theImages=['morhead1', ' morhead2', 'morhead3', 'morhead4', 'morhead5', 'morhead6', 'morhead7', 'morhead8', 'morhead9', 'morhead10', 'morhead11', 'morhead12', 'morhead13', 'morhead14', 'morhead15', 'morhead16', 'morhead17'];
    var p = theImages.length;
    var whichImage = Math.round(Math.random()*(p-1));
    onload=function() {
    document.getElementById('headimg').style.background="url(images/headpics_cal/"+theImages[whichImage]+".jpg) no-repeat";
    }
    both are loaded in to the HTML pages via links.

  2. #2
    SitePoint Member
    Join Date
    Aug 2006
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    addEvent is your friend. If you are using addEvent you can simply change your window.onloads to addEvent( window, 'load', function ).

    Go read that article and digest

  3. #3
    SitePoint Addict
    Join Date
    Mar 2003
    Location
    UK
    Posts
    245
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Scimon
    addEvent is your friend. If you are using addEvent you can simply change your window.onloads to addEvent( window, 'load', function ).

    Go read that article and digest
    Let me get this clear in my mind?
    So I link to the addevent javascript (pasted straight from page you linked) on the ALL the pages then update the other scripts to call that, in the method described above?

  4. #4
    SitePoint Member
    Join Date
    Aug 2006
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yup.

  5. #5
    SitePoint Addict
    Join Date
    Mar 2003
    Location
    UK
    Posts
    245
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cant get it to work?
    the part to be changed in the top ('sfhover') script should be to this?
    Code:
    if (document.all&&document.getElementById) addEvent( window, 'load', function )=sfHover;
    and the one in the lower script should be?
    Code:
    addEvent( window, 'load', function )=function() {
    document.getElementById('headimg').style.background="url(images/headpics_cal/"+theImages[whichImage]+".jpg) no-repeat";
    is this correct, as it doesn't seam to be working?
    I have got the addevent.js loading first (before the other jscripts) in the html, is there other mods I have to make to the jscripts?

  6. #6
    SitePoint Member
    Join Date
    Aug 2006
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ... No. The first one should be:

    if (document.all&&document.getElementById) addEvent( window, 'load', sfHover )

    And the second:

    addEvent( window, 'load', function() {
    document.getElementById('headimg').style.background="url(images/headpics_cal/"+theImages[whichImage]+".jpg) no-repeat"; } );

  7. #7
    SitePoint Addict
    Join Date
    Mar 2003
    Location
    UK
    Posts
    245
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up

    I see, I misunderstood the instruction, not done much javascript stuff.
    Working now, thanks a lot.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •