Solved

Using URL variables in JS

Posted on 2006-06-20
2
348 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
Comment Utility

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
Comment Utility
Thanks. That's great. It's helped me a lot.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
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…

763 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

6 Experts available now in Live!

Get 1:1 Help Now