views:

353

answers:

3

Suppose I have this url:

s = 'http://mydomain.com/?q=microsoft&p=next'

In this case, how do I extract "microsoft" from the string? I know that in python, it would be:

new_s = s[s.find('?q=')+len('?q='):s.find('&',s.find('?q='))]
+1  A: 
(function(){

    var url = 'http://mydomain.com/?q=microsoft&p=next'
    var s = url.search.substring(1).split('&');

    if(!s.length) return;

    window.GET = {};

    for(var i  = 0; i < s.length; i++) {

        var parts = s[i].split('=');

        GET[unescape(parts[0])] = unescape(parts[1]);

    }

}())

Think this will work..

ArneRie
`decodeURIComponent`. `escape`/`unescape` is almost always a mistake.
bobince
+1  A: 

Does the answer here help:

stackoverflow.com/questions/827368/

<script>
var param1var = getQueryVariable("param1");

function getQueryVariable(variable) {
  var query = window.location.search.substring(1);
  var vars = query.split("&");
  for (var i=0;i<vars.length;i++) {
    var pair = vars[i].split("=");
    if (pair[0] == variable) {
      return pair[1];
    }
  } 
  alert('Query Variable ' + variable + ' not found');
}
</script>

Upvote the guy in the link if it does!

Rew