Location.href not working

The command document.location.href=”www.google.com” is not working in google chrome browser.
So your question would be Why Location.href not working?

Well its depends how you’re trying to use location.href. Sometimes, we put button inside the form element with action property empty or with ‘javascript:;’ (please take a look)  in order to redirect user one page to another. So in this case location.href could create problem.

Location.href not working

 

So there are two ways in order to fix this problem.

1. Avoid Using Form Element

You can put your button outside form, if there is no other way, you have to kept button inside form then change the button type to ‘Button’, I think by default its submit.

2. Use Simple Code

window.event.returnValue = false;

This one line of code can fix this issue but this only works in Google Chrome not in Firefox, you have to add some checks like if browser is Chrome or not if yes then execute above code else you can use simple location.href.

Other case when location.href does not work

Sometimes in IE, location.href does not work because of cache problem, I already created a post related to IE cache problem. You can get more information about this topic here

Hope this will help you to fix your same problem.

One Response to “Location.href not working”
  1. Windy June 25, 2017

Leave a Reply