1

Say I have http://www.mysite.com/index.php?=332

Is it possible to retrieve the string after ?= using jQuery? I've been looking around Google only to find a lot of Ajax and URL vars information which doesn't seem to give me any idea.

if (url.indexOf("?=") > 0) {
  alert('do this');
} 
Felix Kling
  • 756,363
  • 169
  • 1,062
  • 1,111
Liam
  • 9,537
  • 36
  • 106
  • 204

4 Answers4

1

window.location is your friend

Specifically window.location.search

u.k
  • 3,071
  • 1
  • 19
  • 23
0

First your query string is not correct, then you can simply take the substring between the indexOf '?=' + 1 and the length of the string. Please see : http://www.w3schools.com/jsref/jsref_substring.asp

When it is easy to do without JQuery, do it with js only.

JohnJohnGa
  • 15,076
  • 17
  • 60
  • 86
0
var myArgs = window.location.search.slice(1)
var args = myArgs.split("&") // splits on the & if that's what you need
var params = {}
var temp = []
for (var i = 0; i < args.length; i++) {
    temp = args[i].split("=")
    params[temp[0]] = temp[1]
}

// var url = "http://abc.com?a=b&c=d"
// params now might look like this:
//  {
//     a: "a",
//     c: "d"
//  }

What are you trying to do? You very well may be doing it wrong if you're reading the URL.

Jamund Ferguson
  • 16,173
  • 3
  • 41
  • 49
0

here is a code snippet (not by me , don't remember the source) for returning a value from a query string by providing a name

$.urlParam = function(name){
 var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(window.location.href);        
 if (!results) 
      { return 0; }        
 return results[1] || 0;

}

Nadeem Khedr
  • 5,163
  • 3
  • 27
  • 37