Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Retrieving value from a URL in a javascript

Posted on 2006-10-27
4
Medium Priority
?
327 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
[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
4 Comments
 
LVL 1

Accepted Solution

by:
whatstheproblem earned 100 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
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 …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

604 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