PDA

View Full Version : How to Onload a function if no access to Body Tag



Wuteverx1972
02-02-2011, 08:09 PM
Hey guys I am having trouble loading a drop down menu list script that I got from Dynamic Drive.

Here's the original test page. (Works Fine)


<html>
<head>
<script language="javascript" src="http://www.modbargains.com/images/Javascript/ChainSel/chainedmenu.js">
</script>

<script language="javascript" src="http://www.modbargains.com/images/Javascript/ChainSel/MBVehicleChainSelect.js"></script>

</head>
<body onLoad="initListGroup('Vehicles', document.listmenu0.firstlevel, document.listmenu0.secondlevel, document.listmenu0.thirdlevel, 'savestate')">

<form name="listmenu0">
<table align="center">
<tr>
<td><select name="firstlevel" style="width:160px;"></select></td>
</tr>
<tr>
<td><select name="secondlevel" style="width:160px;"></select></td>
</tr>
<tr>
<td><select name="thirdlevel" style="width:160px;"></select></td>
</tr>
<tr>
<td><input type="button" value="Go" onClick="goListGroup(document.listmenu0.firstlevel, document.listmenu0.secondlevel, document.listmenu0.thirdlevel)">
<input type="button" value="Reset" onClick="resetListGroup('Vehicles')">
</tr>
</table>
</form>

</body>
</html>

HOWEVER! The site I am working on (which is on a different server) does not allow me to alternate the body tag.

Is there an alternative way of doing a body onLoad without actually alternating the body tag?

I read that you could do a function onload using javascript, so I tried using this:

<script language="javascript" >
window.onload = initListGroup('Vehicles', document.listmenu0.firstlevel, document.listmenu0.secondlevel, document.listmenu0.thirdlevel, 'savestate');
</script>

I am not sure if the function is correct but it does not work. Any ideas how I can make this work????

leetvivi
02-02-2011, 08:17 PM
window.onload = function () {
eventSelected(strDate);
}

This is just a snippet of one of my codes I'm working on. However you said you have tried this and it's still not working. Mine on the other hand is for some reason.

Wish I could help more :(

devnull69
02-02-2011, 08:26 PM
The difference between your call and leetvivi's call is that leetvivi's call is correct.

window.onload has to be assigned to a function reference. leetvivi's code is doing that by handing over an anonymous function. In your code you are assigning the return value (the "result") of the function call, which is wrong.


window.onload = function() {
initListGroup('Vehicles', document.listmenu0.firstlevel, document.listmenu0.secondlevel, document.listmenu0.thirdlevel, 'savestate');
}

Wuteverx1972
02-02-2011, 09:09 PM
The difference between your call and leetvivi's call is that leetvivi's call is correct.

window.onload has to be assigned to a function reference. leetvivi's code is doing that by handing over an anonymous function. In your code you are assigning the return value (the "result") of the function call, which is wrong.


window.onload = function() {
initListGroup('Vehicles', document.listmenu0.firstlevel, document.listmenu0.secondlevel, document.listmenu0.thirdlevel, 'savestate');
}


This is what I have right now using the code you provided but it's still not working. What am I doing wrong?


<head>
<script language="javascript" src="http://www.modbargains.com/images/Javascript/ChainSel/chainedmenu.js">
</script>

<script language="javascript" src="http://www.modbargains.com/images/Javascript/ChainSel/MBVehicleChainSelect.js"></script>

<script language="javascript" >
window.onload = function() {
initListGroup('Vehicles', document.listmenu0.firstlevel, document.listmenu0.secondlevel, document.listmenu0.thirdlevel, 'savestate');
}
</script>
</head>

Here's the body tag that I cannot edit

<body onLoad="javascript:init();">

devnull69
02-02-2011, 10:04 PM
Hm, it's possible that the onload handler of the body tag is overwriting the window.onload just because the body tag will be loaded after the javascript.

Try to put the window.onload down below right before the </body> and see if that changes anything ...

rnd me
02-02-2011, 10:16 PM
document["v"!="\v" ? "addEventListener" : "attachEvent"](
("v"!="\v" ? "" : "on") + "load",
function() {
initListGroup('Vehicles', document.listmenu0.firstlevel, document.listmenu0.secondlevel, document.listmenu0.thirdlevel, 'savestate');
}
)

Wuteverx1972
02-02-2011, 11:14 PM
I just noticed that whenever place my script code, it causes an error on the page. The website I'm working for is an eCommerce website and whenever I enable this script, it messes up other functions on the site like the "Buy it now" button.

Here is the error code I get whenever I enable the script.


Webpage error details

User Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; GTB6.5; SLCC1; .NET CLR 2.0.50727; InfoPath.2; .NET CLR 3.5.30729; .NET CLR 3.0.30618)
Timestamp: Wed, 2 Feb 2011 23:02:11 UTC


Message: Syntax error
Line: 1436
Char: 1
Code: 0
URI: http://www.modbargains.com/

Here's an Image of the code whenever I enable it on the website

http://www.csupomona.edu/~hlugo/Other/ChainSelect.jpg

rnd me
02-02-2011, 11:23 PM
you have an extra "}" at the end...

Wuteverx1972
02-02-2011, 11:37 PM
I just removed it and it's still not working but there is no error now...

I just noticed something else though...

When I enable the main code to display my script, it prevents users from clicking on the "Buy it Now" button. I mean you can click on it but it won't direct you to the shopping cart like its supposed to. Instead it does nothing. Is there something wrong with my form?

<form name="listmenu0">
<table align="center">
<tr>
<td><select name="firstlevel" style="width:160px;"></select></td>
</tr>
<tr>
<td><select name="secondlevel" style="width:160px;"></select></td>
</tr>
<tr>
<td><select name="thirdlevel" style="width:160px;"></select></td>
</tr>
<tr>
<td><input type="button" value="Go" onClick="goListGroup(document.listmenu0.firstlevel, document.listmenu0.secondlevel, document.listmenu0.thirdlevel)">
<input type="button" value="Reset" onClick="resetListGroup('Vehicles')">
</tr>
</table>
</form>

Wuteverx1972
02-02-2011, 11:58 PM
Here is a link to where I got my Chain Selects. Let me know if you guys can figure it out. Keep in mind that I don't have access to the body tag

http://www.yxscripts.com/cs/chainedselects.html

rnd me
02-03-2011, 12:53 AM
oops, i said document.but since that's not an element, it didn't work.
normally, i used body, but since this has to go in the head, body's out.

sorry about that, this should work:


window["v"!="\v" ? "addEventListener" : "attachEvent"](
("v"!="\v" ? "" : "on") + "load",
function() {
initListGroup('Vehicles', document.listmenu0.firstlevel, document.listmenu0.secondlevel, document.listmenu0.thirdlevel, 'savestate');
}
)

Wuteverx1972
02-04-2011, 07:06 PM
Still not working.... do you know any other methods of achieving this chain select?