?
Solved

Using URL variables in JS

Posted on 2006-06-20
2
Medium Priority
?
354 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 7

Accepted Solution

by:
geordie007 earned 200 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…
Suggested Courses

771 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