16

Possible Duplicate:
Get query string values in JavaScript

how can i get the get variable in javascript?

i want to pass it in jquery function.

function updateTabs(){

            //var number=$_GET['number']; how can i do this?
        alert(number);
        $( "#tabs" ).tabs({ selected: number });

    }
Community
  • 1
  • 1
Nirali Joshi
  • 1,968
  • 6
  • 30
  • 50

3 Answers3

57
var $_GET = {};
if(document.location.toString().indexOf('?') !== -1) {
    var query = document.location
                   .toString()
                   // get the query string
                   .replace(/^.*?\?/, '')
                   // and remove any existing hash string (thanks, @vrijdenker)
                   .replace(/#.*$/, '')
                   .split('&');

    for(var i=0, l=query.length; i<l; i++) {
       var aux = decodeURIComponent(query[i]).split('=');
       $_GET[aux[0]] = aux[1];
    }
}
//get the 'index' query parameter
alert($_GET['index']);
gion_13
  • 41,171
  • 10
  • 96
  • 108
9

Write:

var $_GET=[];
window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi,function(a,name,value){$_GET[name]=value;});

Then $_GET['number']

Alexandre Khoury
  • 3,896
  • 5
  • 37
  • 58
  • 1
    Don't create variables in JS without `var`. I'd recommend avoiding starting variable names with `$` in JS, it isn't typical. Don't use arrays to hold key/value pairs, use plain objects for that. – Quentin Aug 21 '12 at 06:58
  • 1
    @Quentin: The dollar-sign in front of variables is typically used as a convention for jquery objects. `var $rows = $("tr")` for example. – Jordan Arsenault Aug 21 '12 at 07:01
  • @JordanArseno — I never said it wasn't allowed by the standard, just that it wasn't *typical* for JavaScript. – Quentin Aug 21 '12 at 07:02
-3

Try this

if (location.search != "")
{
    var x = location.search.substr(1).split(";")
    for (var i=0; i<x.length; i++)
    {
         var y = x[i].split("=");
         alert("Key '" + y[0] + "' has the content '" + y[1]+"'")
    }
}
Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Siva
  • 481
  • 7
  • 26