Click to See Complete Forum and Search --> : [RESOLVED] Script works in IE7, not Firefox 2.0?


arzo2000
01-03-2007, 10:23 AM
I have the following script which is suppose to grab all parameters (and their values) passed in through query string, put them in an array, and display the parameter value in any text field on a page that has its "title" attribute the same as the parameter name.

The problem is that this script only works in IE and not Firefox. Is there a way to make it compatible for both? Or is there a function that I am using in this script that does not work in FF?

Any help is greatly appreciated guys. Thanks in advance.


// GET AN ARRAY OF PASSED PARAMETERS...
var qsParm = new Array();
var query = window.location.search.substring(1);
var parms = query.split('&');
for (var i=0; i<parms.length; i++) {
var pos = parms[i].indexOf('=');
if (pos > 0) {
var key = parms[i].substring(0,pos);
var val = parms[i].substring(pos+1);
qsParm[key] = val;
}
}

// GET ALL ELEMENTS ON HTML DOCUMENT...
var elementColl = document.body.all;
var i;
for (i = 0; i < elementColl .length; i++)
{
if (elementColl[i] != null && elementColl[i].getAttribute("title") != null)
{
// CHECK IF PASSED PARAMETER [ARRAY KEY] MATCHES A TITLE ATTRIBUTE ON ONE OF THE HTML ELEMENTS...
var x = elementColl[i].getAttribute("title");
if (qsParm[x] != null)
{
// SET THE ELEMENT VALUE TO THE PARAMETER VALUE [ARRAY VALUE]...
elementColl[i].value = qsParm[x];
//elementColl[i].parentNode.parentNode.parentNode.style.display = "none";
}
}
}

thechasboi
01-03-2007, 10:32 AM
arzo2000

You might want to alert the string you get. I think it contains a '?'. If it does you can eliminate it with regular expression. regText1 = new RegExp('?', g); parms =parms.replace(regText1 , '');
this will get rid of all your '?' in the string.

mrhoo
01-03-2007, 10:34 AM
document.body.all isn't necessary in IE7, or possible in firefox.

use document.getElementsByTagName('*') to return a list of the elements on your page.

arzo2000
01-03-2007, 10:47 AM
getElementsByTagName did the trick but obviously with only one specific type of elements (i.e input or textarea). Is there a way to get multiple element types with getElementsByTagName or another way to grab more than one type?

Thanks guys.

thechasboi
01-03-2007, 10:51 AM
You could use a set of arrays. The first array could have the ID's and the second could have the tag types. With a few case or if statements you could iterate through the second array in a for loop based upon which ID you would like to use. It might be a good idea to use a browser detection script and adjust your script for browsers that do not have DOM and such. Safari behaves rather weirdly even though it has a DOM in some cases. That is if your users use Mac. It is a pain I know. Hope this helps.

arzo2000
01-03-2007, 11:00 AM
Thanks thechasboi for the tip. I guess I'll take that into consideration. Although a painful way as you said, but does the job.

Thanks again.

arzo2000
01-03-2007, 11:13 AM
By the way guys, I found a way around this and a simpler way than creating more arrays for each type.

Since all my elements are wrapped in one form, I used:
document.forms["formName"].elements

and that worked for both IE and Firefox.

Thanks for all your tips, its greatly appreciated.