?
Solved

write a javascript value from a url

Posted on 2011-10-30
13
Medium Priority
?
301 Views
Last Modified: 2012-06-21
If I have a page displayed which has the link  test.php?thenumb=8

What is the quickest way to put the thenumb value in  the body of a html file.
0
Comment
Question by:debbieau1
  • 5
  • 5
  • 3
13 Comments
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 37054770
I would use PHP to do it, not javascript.
<?php
// first get the value after checking it
if(isset($_GET['thenumb'])) $thenumb = $_GET['thenumb'] else $thenumb = "";
?>

Then later in the page where you need it use something like:

<div>The number is <?php echo $thenumb; ?></div>

Open in new window

0
 
LVL 1

Author Comment

by:debbieau1
ID: 37054779
I know php well thanks.  I was hoping for a javascript solution for this. I am doing an offline app and want it to to render client side and not go via php interpreter.
0
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 37054827
test.php?thenumb=8  

Is that the address of the page or just a link on it?

You can use the 'document URL' http://www.w3schools.com/jsref/prop_doc_url.asp and split it http://www.w3schools.com/jsref/jsref_split.asp into an array at the question mark.  Take the second part and get the value from it.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:debbieau1
ID: 37054829
the test.php?thenumb=8 is the url address, so the thenumb is the get parameter.
0
 
LVL 1

Author Comment

by:debbieau1
ID: 37054832
the first link you sent me gets the full url.  I would think in javascript there must be a simple to get the url parameter like in php using $_GET
0
 
LVL 4

Expert Comment

by:happysunny
ID: 37054837
I believe you can do this:

function gup( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

Open in new window


and then this:
var number = gup( 'thenumb' );

Open in new window


Here is where I got the above code from:  http://www.holyguard.net/en/detail/javascript/get-url-parameters-using-javascript/2/63/258/

Hope that helps :)
0
 
LVL 84

Accepted Solution

by:
Dave Baldwin earned 2000 total points
ID: 37054848
Here's a simple demo page that shows how to do it with javascript.  Save it, open it, then add the query string to the end of it and press enter.  The value will show up in the input text box.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<title>JS query string</title>
</head>
<body>
<h1>JS query string</h1>

<input name="hello" id="hello" type="text" value="">
<script type="text/javascript">
<!--
var nustr = location.href;
//document.write(nustr+" ");
var nuarr = new Array();
nuarr = nustr.split("=");
//document.write("|"+nuarr[1]+"|");
document.getElementById('hello').value = nuarr[1];
// -->
</script>

</body>
</html>

Open in new window

0
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 37054859
There is not a simple way like PHP's $_GET available in javascript.  My post above shows the most direct way I could find for another question.
0
 
LVL 4

Expert Comment

by:happysunny
ID: 37054866
Yes, the link I posted says this, "Most of the server-side programming languages that I know of like PHP, ASP, or JSP give you easy access to parameters in the query string of a URL. Javascript does not give you easy access. With javascript you must write your own function to parse the window.location.href value to get the query string parameters you want."
0
 
LVL 1

Author Comment

by:debbieau1
ID: 37054870
thanks that is what I after.  

I see now why you suggested using php.  A lot of code to get simple bit of data

I surpised so much code is  needed to access this.  I have passed javascript variables before in forms and it is really straight forward to access.

Thanks for all your efforts
0
 
LVL 4

Expert Comment

by:happysunny
ID: 37054876
It can never be easy, can it?  ;)
0
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 37054878
You're welcome, glad to help.  Javascript handles values with 'id's pretty but working with the location.href doesn't seem to have any shortcuts.
0
 
LVL 1

Author Comment

by:debbieau1
ID: 37054879
Of course in forms I am using php to access the variables passed.

Thanks again
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

839 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