Solved

Using URL variables in JS

Posted on 2006-06-20
2
352 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
href return to normal window 5 31
Validating Date Part2 2 27
how can i break an array into a couple 5 25
How do I add counter to html listbox using jquery 2 26
This article discusses how to create an extensible mechanism for linked drop downs.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

749 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