Solved

Using URL variables in JS

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

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

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

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

726 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