Nidheesh
Nidheesh

Reputation: 4562

Passing a variable before location.reload()

I was trying to pass a variable before reload in javascript. If I used the following code,

  var myUrl = location.href;
  if (myUrl.substring(myUrl.length-1) == "#")
  {
  myUrl = myUrl.substring(0, myUrl.length-1);
  }
  myUrl =myUrl+"&abc=Y&bdc=N";
  location.href =myUrl;             
  location.reload(); 

This time I am not getting correct url.ie,I am not getting &abc=Y&bdc=N in the URL. If I put the alert like this,

var myUrl = location.href;
if (myUrl.substring(myUrl.length-1) == "#")
{
myUrl = myUrl.substring(0, myUrl.length-1);
}
myUrl =myUrl+"&abc=Y&bdc=N";
location.href =myUrl;
alert(location.href);
location.reload(); 

Upvotes: 1

Views: 4246

Answers (2)

Ghanshyam Lakhani
Ghanshyam Lakhani

Reputation: 372

Remove

location.href =myUrl;
location.reload();

Use only

window.location.href = window.location.href;

Upvotes: -1

Amit Garg
Amit Garg

Reputation: 3907

Remove

location.href =myUrl;
location.reload(); 

Use only

window.location =myUrl;

Upvotes: 3

Related Questions