0

I am looking to use javascript to extract the GET parameters from a user inputed url.

For example is a user enters a url say:

http://www.youtube.com/watch?v=ee925OTFBCA

I could get the v parameter

'ee925OTFBCA' as a variable

Thanks in Advance.

1
  • there are so many open questions on this, did you even bother looking? Commented May 16, 2010 at 13:06

4 Answers 4

1

This should do the trick

// include this somewhere available 
var Query = (function(){
    var query = {}, pair, search = location.search.substring(1).split("&"), i = search.length;
    while (i--) {
        pair = search[i].split("=");
        query[pair[0]] = decodeURIComponent(pair[1]);
    }
    return query;
})();


var v= Query["v"]

This only runs its computation once and creates an object with name/value pairs corresponding to those supplied as parameters

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

4 Comments

Nice one - and only needs to decode once as opposed to the other posts here.
This doesn't work for multiple values. It works to answer this specific question, but not as a general purpose solution.
Guffa: what? Go read up on your DOM/javascript. The search field was never intended to convey multiple parameters with identical names.
The browser happily submits multiple values for identical names, and cgis can handle them fine too, even though perl modules like CGI.pm presume you cannot have multiple identical names.
1

From here:

function getURLParam(strParamName){
  var strReturn = "";
  var strHref = window.location.href;
  if ( strHref.indexOf("?") > -1 ){
    var strQueryString = strHref.substr(strHref.indexOf("?")).toLowerCase();
    var aQueryString = strQueryString.split("&");
    for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
      if (
aQueryString[iParam].indexOf(strParamName.toLowerCase() + "=") > -1 ){
        var aParam = aQueryString[iParam].split("=");
        strReturn = aParam[1];
        break;
      }
    }
  }
  return unescape(strReturn);
}

To use it:

 var v = getURLParam('v')

1 Comment

location.search gives you the ?xxxx part - if no location.search, no questionmark there
0

You can use a function like this:

function querystring(key) {
   var re=new RegExp('(?:\\?|&)'+key+'=(.*?)(?=&|$)','gi');
   var r=[], m;
   while ((m=re.exec(document.location.search)) != null) r.push(m[1]);
   return r;
}

Example:

var v = querystring('v')[0];

The function returns an array with all the values found in the query string. If you have a query string like ?x=0&v=1&v=2&v=3 the call querystring('v') returns an array with three items.

1 Comment

window.location.search is the one to use in my opinion document.location is now document.URL
0

This is my simple snippet:

function extractParamValue(url, name) {
    var pos = url.indexOf(name+'=')+name.length+1;
    var value = url.substring(pos, url.indexOf('&', pos));
    return value;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.