Click to See Complete Forum and Search --> : working solution to parsing values and javascript links


polmsted
10-30-2003, 05:53 PM
I now have two working features, but only one gives me true flexability in capturing variables. Here is code below. not tested on older browsers but it does work on Netscape 7 and IE 6.

If you connect to the code below from a link like:

codebelow.html?blah1=blah2&blah3=blah4

than the links values are saved and then passed on to future pages.

here it is:

<html>
<head>

<script language="JavaScript">
function LoadPage(thePage){
var qs = location.search.substring(1);
var nv = qs.split('&');
var url = new Object();
for(i = 0; i < nv.length; i++)
{
eq = nv[i].indexOf('=');
url[nv[i].substring(0,eq).toLowerCase()] = unescape(nv[i].substring(eq + 1));
}

alert(thePage + '?' + unescape(qs));
location.href = thePage + '?' + unescape(qs);
}

</script>

</head>

<body>

<a href=javascript:LoadPage("page1.html")>page 1</a>

<a href=javascript:LoadPage("page3.html")>page</a>

</body>
</html>

Jeff Mott
10-30-2003, 09:58 PM
You should perform some testing for how your script handles checkboxes and multiple select menus.

Other things to make note of:
* The W3C recommends the semi-colon be allowed in place of an ampersand. (http://www.w3.org/TR/html401/appendix/notes.html#h-B.2.2)
* unescape() will not transpose plus signs to spaces.
* The name portion of the name/value pairs is also URL encoded and should be decoded.

polmsted
10-31-2003, 01:37 PM
I showed the code to a friend and he helped me simplify it.

This works well (on IE and Netscape, including Netscape 4.7)

<html>
<head>
<script>

function LoadPage(thePage){

location.href = thePage + unescape(location.search);

}
</script>

</head>

<body>

<a href=javascript:LoadPage("page3.html")>page3</a>

</body>
</html>