Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Regular Coder tpeck's Avatar
    Join Date
    Oct 2002
    Location
    Sydney, Australia
    Posts
    815
    Thanks
    43
    Thanked 6 Times in 5 Posts

    window onloads not working together

    How can I add these two window.onloads together:

    (they are in different external js files)

    Code:
    if (ie||ns4||ns6)   
    window.onload=onad
    and

    Code:
    window.onload = function()
    {
    	var theInputs1 = document.getElementsByTagName('input');
    	for(var i = 0; i < theInputs1.length; i++)
    	{
    		if(theInputs1[i].type == 'text')
    		{
    			theInputs1[i].onkeyup = function()
    			{
    				createCookie(this.name,this.value,9999);
    			}
    		}
    		if(readCookie(theInputs1[i].name+'val') != null)
    		{
    			theInputs1[i].value = readCookie(theInputs1[i].name+'val');
    		}
    	}
    	
    	var theInputs2 = document.getElementsByTagName('textarea');
      for(var i = 0; i < theInputs2.length; i++)
      {
          theInputs2[i].onkeyup = function()
          {
            createCookie(this.name,this.value,9999);
          }
        if(readCookie(theInputs2[i].name+'val') != null)
        {
          theInputs2[i].value = readCookie(theInputs2[i].name+'val');
        }
      }
    }
    I've tried putting the second piece of code in with the first external file, but

    window.onload = onad;function() { etc.

    ...does not work.

    Besides, the if (ie||ns4||ns6) is not being obeyed.

    It must be possible surely?
    The difference between genius and stupidity is that genius has its limits. (Albert Einstein)

  • #2
    New Coder TripperTreats's Avatar
    Join Date
    Oct 2006
    Posts
    92
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The top level function of an event handler can only be a single function. You would either need to put

    Code:
     if (ie||ns4||ns6) window.onload = onad
    inside of the other long function, or make that long function a separate one called onloadFn(). Then somewhere after the end of all your javascript you would need one overall onload:

    Code:
     window.onload = function () {
      if (ie||ns4||ns6) window.onload = onad;
      onloadFn()
    }
    What I do on my site, because I have many onload events which change often, I have a static

    Code:
    window.onload = onloader
    in my script and onloader looks like:

    Code:
    function onloader () {
      function1();
      function2();
      function3();
      etc., etc.
    }
    Psychedelic digital art at www.trippertreats.com.

    "And in the end, the love you take
    is equal to the love you make
    ."

  • #3
    Regular Coder tpeck's Avatar
    Join Date
    Oct 2002
    Location
    Sydney, Australia
    Posts
    815
    Thanks
    43
    Thanked 6 Times in 5 Posts
    Obviously you know what you are doing, but I can't be following it accurately because this does not do both jobs:

    Code:
    <!--
    // ********** button *********************
    var XX=20;   
    var xstep=0 ;   
    var delay_time=20;   
    var YY=0;   
    var ch=0;   
    var oh=0; 
    var yon=0;   
    var ns4=document.layers?1:0   
    var ie=document.all?1:0   
    var ns6=document.getElementById&&!document.all?1:0   
    if(ie){   
    YY=document.body.clientHeight;mbutton.style.top=YY;   
    }   
    else if (ns4){   
    YY=window.innerHeight;   
    document.mbutton.pageY=YY;   
    document.mbutton.visibility="hidden";   
    }   
    else if (ns6){   
    YY=window.innerHeight   
    document.getElementById('mbutton').style.top=YY   
    }   
    function reloc1()   
    {   
    if(yon==0){YY=YY-xstep;}   
    else{YY=YY+xstep;}   
    if (ie){   
    ch=document.body.clientHeight;   
    oh=mbutton.offsetHeight;   
    }   
    else if (ns4){   
    ch=window.innerHeight;   
    oh=document.mbutton.clip.height;   
    }   
    else if (ns6){   
    ch=window.innerHeight   
    oh=document.getElementById("mbutton").offsetHeight   
    }   
    if(YY<0){yon=1;YY=0;}   
    if(YY>=(ch-oh)){yon=0;YY=(ch-oh);}   
    if(ie){   
    mbutton.style.right=XX;   
    mbutton.style.top=YY+document.body.scrollTop;   
    }   
    else if (ns4){   
    document.mbutton.pageX=XX;   
    document.mbutton.pageY=YY+window.pageYOffset;   
    }   
    else if (ns6){   
    document.getElementById("mbutton").style.right=XX 
    document.getElementById("mbutton").style.top=YY+window.pageYOffset   
    }   
    }   
    function onad()   
    {   
    if(ns4)   
    document.mbutton.visibility="visible";   
    loopfunc();   
    }   
    function loopfunc()   
    {   
    reloc1();   
    setTimeout('loopfunc()',delay_time);   
    }
    
    // ************** cookie ************************
    
    function createCookie(name,whatval,days)
    {
    	if(whatval != '')
    	{
    		if (days)
    		{
    			var date = new Date();
    			date.setTime(date.getTime()+(days*24*60*60*1000));
    			var expires = "; expires="+date.toGMTString();
    		}
    		else var expires = "";
    		document.cookie = name+"val="+whatval+expires+"; path=/";
    	}
    }
    
    function readCookie(name)
    {
    	var nameEQ = name + "=";
    	var ca = document.cookie.split(';');
    	for(var i=0;i < ca.length;i++) {
    		var c = ca[i];
    		while (c.charAt(0)==' ') c = c.substring(1,c.length);
    		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    	}
    	return null;
    }
    
    function onloadFn()
    {
    	var theInputs1 = document.getElementsByTagName('input');
    	for(var i = 0; i < theInputs1.length; i++)
    	{
    		if(theInputs1[i].type == 'text')
    		{
    			theInputs1[i].onkeyup = function()
    			{
    				createCookie(this.name,this.value,9999);
    			}
    		}
    		if(readCookie(theInputs1[i].name+'val') != null)
    		{
    			theInputs1[i].value = readCookie(theInputs1[i].name+'val');
    		}
    	}
    	
    	var theInputs2 = document.getElementsByTagName('textarea');
      for(var i = 0; i < theInputs2.length; i++)
      {
     //   if(theInputs[i].type == 'text')
     //   {
          theInputs2[i].onkeyup = function()
          {
            createCookie(this.name,this.value,9999);
          }
     //   }
        if(readCookie(theInputs2[i].name+'val') != null)
        {
          theInputs2[i].value = readCookie(theInputs2[i].name+'val');
        }
      }
    }
    
    window.onload = function () {
      if (ie||ns4||ns6) window.onload = onad;
      onloadFn()
    }
        
    //  End -->

    I have combined both files into one, and done as you suggested at the bottom. You can see there are two major things happening...a floating button which should show up, and the reading of some cookies.

    For some reason, the "onad" function doesn't seem to get called. And, if the cookie is called on the page I get an "object required" error in line 14 ("YY=document.body.clientHeight;mbutton.style.top=YY;")

    What have I done wrong do you think?
    The difference between genius and stupidity is that genius has its limits. (Albert Einstein)

  • #4
    New Coder TripperTreats's Avatar
    Join Date
    Oct 2006
    Posts
    92
    Thanks
    0
    Thanked 0 Times in 0 Posts
    This is why the onad function isn't getting called:

    Code:
     window.onload = function () {
         if (ie||ns4||ns6) window.onload = onad;
         onloadFn()
    }
    What's happening is that once the window loads, this overarching function is called. Now, since the window is already loaded, the

    Code:
    if (ie||ns4||ns6) window.onload = onad;
    is too late to tell the window what to do onload. Just replace it with

    Code:
    if (ie||ns4||ns6) onad();
    Fix that, and then let's see whether you still get that "object required" error. If so, we'll tackle that problem next.
    Psychedelic digital art at www.trippertreats.com.

    "And in the end, the love you take
    is equal to the love you make
    ."

  • #5
    New Coder TripperTreats's Avatar
    Join Date
    Oct 2006
    Posts
    92
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You know I just realized that I was the one who typed the original window.onload incorrectly, necessitating my previous post. I'm sorry about that! I hope I didn't sound snooty when I was correcting you when it was actually my previous typo I was correcting. Did you get it to work?
    Psychedelic digital art at www.trippertreats.com.

    "And in the end, the love you take
    is equal to the love you make
    ."

  • #6
    Regular Coder tpeck's Avatar
    Join Date
    Oct 2002
    Location
    Sydney, Australia
    Posts
    815
    Thanks
    43
    Thanked 6 Times in 5 Posts
    Not at all! No worries...

    I'm always amazed at how helpful people are. I'm not much use, but I post these demos so that others might profit from them.

    Here is a demo of the idea you corrected in action.

    (There is still a problem with the two variables somehow saving as the same cookie - ie. the input box and the textarea box don't save separate values. But the onload now seems to be working for both routines, the button and the cookies.)

    http://aapress.com.au/demo/onload/page1.html

    Thanks again.
    The difference between genius and stupidity is that genius has its limits. (Albert Einstein)

  • #7
    Regular Coder tpeck's Avatar
    Join Date
    Oct 2002
    Location
    Sydney, Australia
    Posts
    815
    Thanks
    43
    Thanked 6 Times in 5 Posts
    By the way, Jason, your site only shows the navigation scheme in FF; it doesn't appear to function in IE7. (Maybe in 6, I don't know.) And I get a stack overflow at line 0 error too.

    Nice site though, but only in FF for me. Is it my browser?
    The difference between genius and stupidity is that genius has its limits. (Albert Einstein)

  • #8
    New Coder TripperTreats's Avatar
    Join Date
    Oct 2006
    Posts
    92
    Thanks
    0
    Thanked 0 Times in 0 Posts
    From the time of your post, you may have accessed my site in the middle of my updating it. I have been working on it all day and at various times it was not working (the nature of inchoate coding).

    By the way, do you know what that stack overflow error is all about? I've been getting it a lot, but only in IE.

    Would you do me a really big favor? I don't have (and can't get) IE7, because that takes a valid windows key (which I don't have). Would you mind checking out my site again quickly in IE7?

    I'm hoping that the navigation problem you mentioned was just a result of my interim repairs. By the way, I thought I had a great CSS layout before, but then I came across http://www.positioniseverything.net/...maker_form.php. You should check it out! My new and improved products page is thanks to it. Thanks so much for your reciprocal help with my site! Isn't this forum great?
    Psychedelic digital art at www.trippertreats.com.

    "And in the end, the love you take
    is equal to the love you make
    ."

  • #9
    Regular Coder tpeck's Avatar
    Join Date
    Oct 2002
    Location
    Sydney, Australia
    Posts
    815
    Thanks
    43
    Thanked 6 Times in 5 Posts
    I think it's my problem. Perhaps I haven't got Flash installed correctly. Sometimes flash works for me in IE7 but not always. Reinstalling it does nothing. I don't think it's IE7's problem, but it's something I've done.

    Maybe it's a bit dicey having a nav bar that is only flash, though - perhaps both a flash nav bar AND an ordinary one down the bottom (or my next/previous idea!)
    The difference between genius and stupidity is that genius has its limits. (Albert Einstein)


  •  

    Posting Permissions

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