?
Solved

Retrieving value from a URL in a javascript

Posted on 2006-10-27
4
Medium Priority
?
326 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

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.
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
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 fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Suggested Courses

752 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