PDA

View Full Version : add triger to window.onload = function()



simplifyit
08-12-2006, 04:47 AM
Hello all,

I have in the past been loading a .js file at the bottom of the page to perfom a function on my menu format and layout, however i have been cleaning up my code lately and have been moving alot of functions to "window.onload = function()"

My question is this: Can someone help me out with the best way to add the code below to the window.onload = function() { } of my .js file that i load at the top of the page.... I have been playing with this for weeks, however i think i am too close to it and can't seem to find where i am going wrong.



function addEvent(obj, evType, fn){
if (obj.addEventListener){
obj.addEventListener(evType, fn, true);
return true;
} else if (obj.attachEvent){
var r = obj.attachEvent("on"+evType, fn);
return r;
} else {
return false;
}
}

if (document.getElementById('left_menu')?true:false != false) {
var list = document.getElementById("left_menu").getElementsByTagName("ul");
for(i = 1; i < list.length; i++){
if(String(list[i].parentNode.nodeName).toLowerCase() == 'li') {
addEvent(list[i].parentNode, 'click', doIT);
}
}
}

if (document.getElementById('left_menu'||'top_nav')?true:false != false) {
document.write("<style> div#left_menu ul ul { display:none; } </style>");
var theLink = document.links;
for (var l=0; l < theLink.length; l++)
if (location.href == theLink[l]||location.href == theLink[l]+'#') {
theLink[l].id = 'selected';

var subLink = theLink[l].parentNode.parentNode.parentNode;
var subMenu = theLink[l].parentNode.parentNode;

if (String(subLink.nodeName).toLowerCase() == 'li') {
subLink.id = 'selected';
subMenu.id = 'selected';
}
}
}

function doIT() {
if (document.attachEvent) {
var theEl = window.event.srcElement.parentNode; }
else { var theEl = this; }
u=theEl.getElementsByTagName("ul")[0];
u.style.display=(u.style.display=='none'||u.style.display=='')?'block':'none';
}


Any and all help/suggestions appreciated! :)

jscheuer1
08-12-2006, 06:19 AM
There isn't a single occurrence of 'onload' in your code snippet.

simplifyit
08-12-2006, 06:29 AM
There isn't a single occurrence of 'onload' in your code snippet.
Hi John,

Your right bacause currently i am loading this at the bottom of a page like this...



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="stylesheet" href="includes/style.css" type="text/css">
<script language="JavaScript" type="text/javascript" src="includes/pageLib.js" ></script>
</head>

<body>
content stuff here
</body>
<script language="JavaScript" type="text/javascript" src="includes/bpageLib.js" ></script>
</html>


So the code above in currently in the bpageLib.js and i am also loading at the top of the page pageLib.js that has a window.onload = function() { } area in it that i am loading other things in also... so i want to modify the bpageLib.js code so that i can remove the need to use that and add some updated code to the window.onload = function() are of the pageLib.js file that gets loaded at the top of the page.

Hope this makes sense.

Darren