window.onload in IE6 - does it work?

Hi

I've asked this question in a roundabout way in a couple of other threads but I'm not getting to the bottom of it. It seems pretty straightforward but the info on the web gets pretty complex.
So I thought I'd keep it simple: can you use 'window.onload' in IE6?

I want to use it in the head of a page to initialise 3 functions that are in an external js file.

I can't get it working.  So, if not, what do you do instead?

Thanks
Lady_MAsked:
Who is Participating?
 
jmanGJHS97Connect With a Mentor Commented:
yeah, i think your init example would be just fine.  another alternative is to do something like this:

<head>
  <script src="javascript">
    function init()
    {
      initialize_function_1(args);
      initialize_function_2(args);
      initialize_function_3(args);
    }
  </script>
</head>
<body onload="init(); />
  -- body of your page
</body>

or, you can take the script tag and make it an include, so you don't have your code in the page itself:

<script src="myFolder/jsFiles/init.js" />

and just put the function code in the js file, and call it just like i show or the example shows.
0
 
jmanGJHS97Commented:
0
 
Lady_MAuthor Commented:
Hi thanks for getting back to me.  I've read one of those articles before, but it does my head in.  I don't get what the point of that init() is and where you put your function initiations.  Do you?
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
jmanGJHS97Commented:
you write init() and include it as a js include.  the contents of the function are basically what you wanted to do inside the window.onload event.  the idea here is that you just call a custom function, rather than using window.onload.
0
 
Lady_MAuthor Commented:
Hi again.

But it says that the init file should just contain one line:
init();

Are you saying that isn't the case and actually that init function should list my three function calls?  That makes sense.  

If so, what would the exact syntax in that init() function be please?
init (){
initiate function 1(arg, arg);
initiate function 2 ();
initiate function 3();
}
0
 
jmanGJHS97Commented:
i think you're thinking about this the wrong way.  let's step back.

you wanted to do something using window.onload.  so, you were likely trying something like this:

window.onload(callMyFunction());

then, the body of callMyFunction is whatever JavaScript you wanted to do when the window loaded.

since window.onload is not valid in IE6, you have to put a function call in the HTML somewhere, so that when the page loads, it will do something.  so, you do that <script> include that includes the javascript code containing the init() function.  then, in the init function, you do whatever you were going to do inside the callMyFunction.

does that make sense?

the example i sent you doesn't know what you were trying to do once you did window.onload.  it was just trying to show an alternative method of calling some function once the page loads.  after that, it's up to each person to decide what to do in the function.
0
 
Lady_MAuthor Commented:
Hi jmanGJHS97
Thanks, but I'm afraid I really don't understand.  V. confused.
Could you do me a favour and spell it out to me if you have time please? ie: "put this in the html head, put this in the external js and put this in the init.js page".  
You can't assume I know the syntax either.
Thankyou

So my html head looks like this:
<script type="text/javascript" language="javascript" src="/global/scripts/global.js"></script>
<script>
window.onload = function() { rolloverButtons(); rolloverMenuL2 (); populateBox (1.html', null); };
</script>

And my /global.js looks like this:
// JavaScript Document
<script language="JavaScript" type="text/javascript" defer="defer">
//1.  Rollover  Menu:  Show list on hover.
rolloverButtons = function() {    
alert ("rollover buttons");
      var sfEls = document.getElementById('navbuttons').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"), "");        
            }    
      }
}

//2. Rollover Top Nav Menu:  Show second level of drop-down menu to IE by assigning class to #header #banner ul#navL2 li
rolloverMenuL2 = function() {  
alert ("rollover menu");

      var sfEls = document.getElementById('navL2').getElementsByTagName('LI');
      for (var i=0; i<sfEls.length; i++) {
            sfEls[i].onmouseover=function() {
                  this.className+=" over";
            }
            sfEls[i].onmouseout=function() {
                  this.className=this.className.replace(new RegExp(" over\\b"), "");
            }    
      }
}


//3 Post file into box, uses prototype.js which should be in same directory.
function populatebox (serverPage, obj){      
alert ("populate box");

var objID = "boxtext";  
   new Ajax.Updater(objID, serverPage);

//change tab color
  if(obj != null)
  {
   var links = document.getElementById('box').getElementsByTagName('A');
    for (var i=0; i<links.length; i++) {
      if (links[i].className=='highlighted' && links[i] != obj)  
           links[i].className='plain';  
    }
    obj.className='highlighted';
  }
}
</script>

0
 
jmanGJHS97Commented:
i would say the easiest way to do this is to just take the 3 function calls you have in your window.onload function and put them into the body onload event, like this:

<body onload="rolloverButtons(); rolloverMenuL2 (); populateBox (1.html', null);">

then, just delete this:

<script>
window.onload = function() { rolloverButtons(); rolloverMenuL2 (); populateBox (1.html', null); };
</script>

that should do the same thing for you.  all the example was doing is putting those 3 function calls into another function, but you could just call them in the body tag, like i show here.
0
 
jmanGJHS97Commented:
i'm not sure what this is:

1.html'

there is a trailing single quote, which will cause an error.  also, you can't name an element with a number.

if you have a text box like this:

<input type="text" id="myTextBox" value="" />

then you can call your populateBox like this:

populateBox(document.getElementById('myTextBox).innerHTML, null);

but, this question is about the window.onload event, not debugging your javascript.  that stuff is better suited for another question in EE.
0
 
Lady_MAuthor Commented:
Yes, I really just want to get the initialisation of the external functions working, that's all.  I will worry about code problems later.  (incidentally, the file isn't really called 1.html, that's just for convenience here).

But if you can give me some more help on initialising the 3 functions please, if you have time, that would be much appreciated as that is the major issue at the moment.

Thanks
0
 
jmanGJHS97Commented:
what exactly is not working?  i provided an example that will call the 3 functions when the page loads.  that seemed to me to be the crux of your question.
0
 
Lady_MAuthor Commented:
Hi jmanGJHS97, yes you did, sorry I missed a couple of your replies. I see what you're doing now thanks.
0
 
jmanGJHS97Commented:
Not a problem.  Glad it's working for you.
0
All Courses

From novice to tech pro — start learning today.