Solved

Using URL variables in JS

Posted on 2006-06-20
2
349 Views
Last Modified: 2012-06-27
I have a script (test.htm) and want to use variables in the url with it to call an external page.
I'd call test.htm like this:  test.htm?YOB=2006&strSEX=F&SAL=A

Here's my script so far:

<SCRIPT LANGUAGE="JavaScript">
<!-- Hide from old browsers
// Modify to reflect site specifics
mainserver = "http://example.com/jserver";
target = "/TARGET=/AAMSZ=MULTIPLE/DOB=" + URL.YOB + "/SEX=" + URL.strSEX + "/SALARY=" + URL.SAL;

// Cache-busting and pageid values
random = Math.round(Math.random() * 100000000);
if (!pageNum)
var pageNum = Math.round(Math.random() * 100000000);
document.write('<scr');
document.write('ipt src="' + mainserver + '/acc_random=' + random + target + "/pageid=" + pageNum + '">');
document.write('</scr');
document.write('ipt>');
// End Hide -->
</script>

How do I access the URL variables and how do I default them to 0 if they don't exist in the URL I pass?

Thank you for your time and help.
0
Comment
Question by:Stoke
2 Comments
 
LVL 7

Accepted Solution

by:
geordie007 earned 50 total points
ID: 16941123

you can grab everything after the question mark by referencing location.search. for example, if your url is:

site.com/test.htm?YOB=2006&strSEX=F&SAL=A

location.search would give you "?YOB=2006&strSEX=F&SAL=A". you could then split them down at the ampersand to give you the name value pairs:

var url_vars = location.search;
var pairs = location.search.split('&');

this give you an array, equivalent to this:

var pairs = new Array('YOB=2006','strSEX=F','SAL=A');

you could then split these down further to find your vars and values:

for(i=0;i<pairs.length;i++){
var url_var = pairs[i].split('=')[0];
var url_val = pairs[i].split('=')[1];
// do something to each here
}

obviously then you can reset each one however you want it, as you loop through the values. if you need those values returning, so yould build up a new url - with your values - and then pass it back:

location.href = 'test.htm?' + var1 + '=' + val1 + '&' + var2 + '=' + val2;

hope this helps

0
 

Author Comment

by:Stoke
ID: 16941469
Thanks. That's great. It's helped me a lot.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

27 Experts available now in Live!

Get 1:1 Help Now