13

How can I get the url value after a question mark in javascript?

I want the following behaviour:

if value is yes then 10+2 
else if value is no then 10.

I want to output with alert messages.

<div class="col-xs-4 col-sm-4 col-md-4 text-center width100">
    <a href="piccal.html" class="btn btn-success" id="yes">PIC Grant Calculator</a>
</div>
<div class="col-xs-4 col-sm-4 col-md-4 text-center width100">
    <a href="piccal.html" class="btn btn-danger" id="no">PIC Grant Calculator</a>
</div>

<script> 
if (value == no) {
    alert('hello');
}
</script>

<script> 
if (value == yes) {
    alert('hi');
}
</script>
Danny Staple
  • 7,101
  • 4
  • 43
  • 56
Ashish Mishra
  • 165
  • 1
  • 1
  • 9
  • 2
    possible duplicate of [How can I get query string values in JavaScript?](http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript) – Anonymous Feb 28 '14 at 11:40

2 Answers2

16

use this code:

var a = location.href; 
var b = a.substring(a.indexOf("?")+1);
kp singh
  • 1,430
  • 8
  • 21
12

use javsacript and type:

location.search
AO_
  • 2,573
  • 3
  • 30
  • 31
  • 1
    use `window.location.search` as a variable and it will return you the full string of what is in the URL to the right of the "?" and including it, you may also push data to this location using this method. Andrew's answer is correct – 255.tar.xz May 07 '19 at 22:43