Solved

Retrieving value from a URL in a javascript

Posted on 2006-10-27
4
321 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
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…

920 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

15 Experts available now in Live!

Get 1:1 Help Now