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');
} 
3

4 Answers 4

1

window.location is your friend

Specifically window.location.search

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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;

}

Comments

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.

1 Comment

window.location.href should just be window.location.search

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.