Solved

Retrieving value from a URL in a javascript

Posted on 2006-10-27
4
320 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
Comment Utility
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
Comment Utility
Haven't heard from you, did that work?

Any other question, fell free to ask.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Here we come across an interesting topic of coding guidelines while designing automation test scripts. The scope of this article will not be limited to QTP but to an overall extent of using VB Scripting for automation projects. Introduction Now…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

744 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

16 Experts available now in Live!

Get 1:1 Help Now