14

I can't seem to find any detailed documentation on getParameterByName(). I've searched Mozilla, Google, and here. Am I missing something?

Dandy
  • 851
  • 2
  • 11
  • 22
  • It's a function and I read it wrong. I was trying to see how to collect information from a query string. – Dandy Feb 29 '12 at 15:22
  • 2
    This is what you may be looking for: http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript – Chance Smith Sep 10 '15 at 19:38

4 Answers4

16

We use this where I work. Similar to ThiefMaster's solution...

function getParameterByName( name ){
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return decodeURIComponent(results[1].replace(/\+/g, " "));
}
mtwagner
  • 466
  • 6
  • 12
8

There is no builtin function with this name.

Have a look at How can I get query string values in JavaScript? though - maybe that's the function you are looking for. It returns the querystring parameter with a given name.

Community
  • 1
  • 1
ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
3

You need to add validation of name is not null/undefined

function getParameterByName(name) {
    if (name !== "" && name !== null && name != undefined) {
        name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
        var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
            results = regex.exec(location.search);
        return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
    } else {
        var arr = location.href.split("/");
        return arr[arr.length - 1];
    }

}
Girish Gupta
  • 1,241
  • 13
  • 27
0

because it does not exists. I think you want to look at : getElementsByTagName ? if not provide more information of what kind of method you search

Jerome Cance
  • 8,103
  • 12
  • 53
  • 106