View Full Version : Pass params in body onload=function(x,y) fails in Firefox ... solutions?

05-23-2008, 02:30 PM

This forum has proven to be incredibly useful.

I have a scenario in which i utilize the onload aspect of the body tag to call two functions. One of the functions requires me to pass parameters into it.

This function is called on several different pages, and the parameters fine-tune the results. The first function simply loads some XML.

Below is the code. It functions properly in IE, but does not work in FireFox

<body onLoad="browserDetect();testPhrase2('America\'s Top 100','','','','','AT100')">

I recently learned a few days ago that when utilizeing the object.onLoad method to call a function you have to pass parameters into your function like this (for firefox): NOTE: this syntax does not work in IE?!

xmlDoc.onload = function(evt){
testPhrase2("America's Top 100",'','','','','AT100');

Simply writing the function and parameters works great in IE, but FF requires all these other steps.

I'm guessing my current problem, is once again, not knowing the proper syntax required to pass parameters into a function for firefox when dealing with the body onload event.

I humbly await assistance.

Thank you all very much for you service to others,

05-23-2008, 02:40 PM
Generally what you have in the body tag will work in FF. Perhaps you have some other problem with the code, like if the testPhrase2 function expects the possibility of another argument, the event will be used, even if it is unintended to be so, or the code for the function might just not work with FF anyway. However, as long as the rest of the code is good in FF, you can mimic the type of onload event you have for your xml file, and it will work in IE as well. Get rid of the body onload altogether, and put this script in the head of your HTML page:

<script type="text/javascript">
testPhrase2('America\'s Top 100','','','','','AT100');

05-23-2008, 02:56 PM
Thank you so much.

Your recomendation is actually going to make my life a lot easier, and it solves the problem. In the meatime i found this solution which appears to be working.

<body onLoad="javascript:browserDetect();testPhrase2('America\'s Top 100','AT100','','','','AT100');">

But your other suspision is correct ... my entire application is riddled with problems. The problem is i developed and tested it in IE ... not FF (BIG BIG MISTAKE). The thing works flawlessly in IE, but fails altogether in FF.

I'm going through it problem by problem, and fixing one at a time until hopefully it all works.

Now my problem seems even more ridiculous. Everytime i run into a problem, no error fires in FF - it just doesn't work. Very annoying. I pass a parameter into the function (see above) and use that parameter to compare it to XML data cycling through a for loop. A super simple comparrison is not even working in FF but works great in IE. Can you help with this as well?

for (var i = 0; i < x.length; i++){
var el = x[i];
var nameEl = el.getElementsByTagName('name')[0];
var actualName = nameEl.childNodes[0].nodeValue;
var packageEl = el.getElementsByTagName('package')[0];
var actualPackage = packageEl.childNodes[0].nodeValue;
if (actualPackage==at100){
recordString+=actualName + "-" + actualPackage + "<br />";

I realize this is a code snippet, but the rest of the function works great. As soon as i added the conditional statement the whole thing broke in FF (but not IE).

I thank you in advance for your time and assistance.

05-23-2008, 05:56 PM
Just looking at it, it reminds me of the fact that in FF, there are generally more child nodes than in IE. Even with an otherwise empty element or an element containing only another element, there are text nodes, even if they have no value (text). At the same time, if there is another element inside an element with a text node, the text node may not fall where you assume it does in the DOM tree. So generally it is much easier to walk up the tree than down it as it appears you are doing here. FF comes with something called DOM inspector. Use it to see if the nodes are laid out the way you expect. However, that may all be OK. This:

var nameEl = el.getElementsByTagName('name')[0];

implies a tag:



var packageEl = el.getElementsByTagName('package')[0];

implies one:


Now I'm not 100% sure about 'name' but I am about 'package', and I think they are both invalid. FF may be ignoring them as nodes since they aren't valid tags. Then again, if this is all xml, it should be OK on that score, but there may be a more standard way to get the nodes in that case, I'm not sure, as I don't do much xml coding.

If you are getting a specific error in FF that would probably be a big help in figuring this out. There is a Developer's Extension for FF. It's free and has good tools. However, if I recall correctly, the most useful are the DOM Inspector and the error console, both of which come with FF if you choose for them to be included at installation. There are some handy things and some very useful things though in the extension which are not in regular FF, so I would recommend the extension. The CSS editor and the view scripts feature are really helpful. There is also a view generated source code, which can help you to see what changes to the HTML structure your scripts are having.

Developer Extension:


Dom Inspector: