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
  • 795,719
  • 175
  • 1,089
  • 1,143
Liam
  • 9,725
  • 39
  • 111
  • 209

4 Answers4

1

window.location is your friend

Specifically window.location.search

u.k
  • 3,091
  • 1
  • 20
  • 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,446
  • 19
  • 62
  • 87
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,721
  • 3
  • 42
  • 50
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,273
  • 3
  • 30
  • 37