Get querystring javascript.

gaptrast

Member
Messages
123
Reaction score
0
Points
16
Hello,

How to get the query string in javascript?

Like this:

Code:
<script type="text/javascript">
function getp(){
var p = ????GET P????
document.write('this is ' + p);
}
</script>

<div onclick="getp()">Click to show p</div>

If this website was www.example.com/index.php?p=fun then 'This is fun' had showed up..


How can I get this work?
 

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
First step. To find the full URL of the page, use document.location
HTML:
<html>
<head><title>Test</title>

function qstring(){
   var q = document.location ;
   alert( q );
}
</script>
<body>

<button onclick='qstring();' >Query String</button>
</body>
</html>

Now, you have to figure out how to get the query string from the URL.
 

essellar

Community Advocate
Community Support
Messages
3,295
Reaction score
227
Points
63
The query string lives at window.location.search. That being said, there's a neat little function I posted over at Stack Overflow that will probably save you a lot of work. Let me know if you have any questions about it.
 
Top