Solved

Retrieving value from a URL in a javascript

Posted on 2006-10-27
4
323 Views
Last Modified: 2010-04-16
I want to retrieve value from the URL http://www.xyz.com?as=query in javascript and print the value on the page.
I dont want to use any html tags.
Everything should be in javascript.

Pls provide me the some javascript example since i am not very much comfortable in this langauge.

0
Comment
Question by:bhatt_nishant
  • 2
4 Comments
 
LVL 1

Accepted Solution

by:
whatstheproblem earned 25 total points
ID: 17818092
Here's what I found from about.com :

http://javascript.about.com/library/blqs.htm

You might want to read that for some more information.

The script is :

<a href="test2.html?first=GREEN&second=12345678">Click Here</a>

Use this to point to your second page from your first. Change test2.html to your site address and change GREEN and 12345678 to what ever information you want to carry on.

To get the value into your second page use this script :

<html>
<head>
<script>

var qsParm = new Array();
function qs() {
var query = window.location.search.substring(1);
var parms = query.split('&');
for (var i=0; i<parms.length; i++) {
var pos = parms[i].indexOf('=');
if (pos > 0) {
var key = parms[i].substring(0,pos);
var val = parms[i].substring(pos+1);
qsParm[key] = val;
}
}
}

//List your fields here so they have default values.
qsParm['first'] = null;
qsParm['second'] = null;
qs();

</script>
</head>
<body>

You have entered

<script>

//This will add it to your webpage.
//The "If" checks to make sure they are not empty.

if (qsParm['first'] && qsParm['second'])
document.write(' : ' + qsParm['first'] + ' and ' + qsParm['second']);

</script>

</body>
</html>

Add this to your <HEAD>


var qsParm = new Array();
function qs() {
var query = window.location.search.substring(1);
var parms = query.split('&');
for (var i=0; i<parms.length; i++) {
var pos = parms[i].indexOf('=');
if (pos > 0) {
var key = parms[i].substring(0,pos);
var val = parms[i].substring(pos+1);
qsParm[key] = val;
}
}
}

You can also give your fields default values if you want, add this you your <HEAD> as well :

//List your fields here so they have default values.
qsParm['first'] = null;
qsParm['second'] = null;
qs();

Now to retrieve the information, use this script in your <BODY> tag :

<script>

if (qsParm['first'] && qsParm['second'])
document.write( qsParm['first'] + ' and ' + qsParm['second']);

</script>

Or just use document.write( qsParm['first']); and document.write( qsParm['second']); in a <SCRIPT> tag to get the values by themselves.

Like I said, it's from the "About.com" page here : http://javascript.about.com/library/blqs.htm

Any questions let me know :-)
0
 
LVL 1

Expert Comment

by:whatstheproblem
ID: 17824053
Haven't heard from you, did that work?

Any other question, fell free to ask.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

820 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