1

Possible Duplicate:
Get query string values in JavaScript

I have a URL like below

    http://domain_name/?act=SiteVisit

I want to split this URL and want to take act value in one variable at the time of page load.

$('document').ready(function(){
    var mail_link  = window.location;   
    var arrURL     = mail_link.split('='); 
    alert(arrURL[1]);
    return false;
});

I used the above code but its not working.But if I print and assign the variable which i get in alert message its working

So tell me how to get the SiteVisit in a variable

Community
  • 1
  • 1
user1187
  • 2,116
  • 8
  • 41
  • 74

2 Answers2

1

window.location doesn't return anything. It should be window.location.href

var mail_link  = window.location.href;   
var arrURL = mail_link.split('='); 
alert(arrURL[1]);
return false;

SEE DEMO

Ahsan Khurshid
  • 9,383
  • 1
  • 33
  • 51
1

You could use location.search, which represents the query part of the location (everything after ?):

$('document').ready(function(){
    alert(location.search.split('=')[1]); 
});

If the string consists of more than one parameter (e.g. ?act=SiteVisit&last=yesterday), you can decompose it with something like:

var params = {}, srch = location.search.split('&');
for (var i=0;i<srch.length;i+=1){
  var pair = srch[i].split('=');
  params[pair[0]]  = pair[1];
}
// for ?act=SiteVisit&last=yesterday =>
// params.act = 'SiteVisit'
// params.last = 'yesterday'
KooiInc
  • 119,216
  • 31
  • 141
  • 177