Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

window.location.href doesn't redirect

I know this is a question much discussed but I can't figure out why it does not work for me.

This is my function:

function ShowComments(){   alert("fired");  var movieShareId = document.getElementById('movieId');  //alert("found div" + movieShareId.textContent || movieShareId.innerText);  //alert("redirect location: /comments.aspx?id=" + movieShareId.textContent || movieShareId.innerText + "/");  window.location.href = "/comments.aspx?id=" + movieShareId.textContent || movieShareId.innerText + "/";  var newLocation = window.location;  //alert("full location: " + window.location);  } 

If I have the alerts uncommented or if I have Mozilla's bugzilla open it works fine, otherwise it does not redirect to the other page.

Any ideas why?

like image 271
user2235124 Avatar asked Apr 02 '13 08:04

user2235124


People also ask

What is window location href?

Window Location Href The window.location.href property returns the URL of the current page.

Does window location href reload the page?

window. location. href method returns/loads the current url. So we can use it to reload/refresh the page in javascript.

Does window location href return a string?

href. The href property of the Location interface is a stringifier that returns a string containing the whole URL, and allows the href to be updated.


2 Answers

If you are calling this function through a submit button. This may be the reason why the browser does not redirect. It will run the code in the function and then submit the page instead of redirect. In this case change the type tag of your button.

like image 105
Jose Avatar answered Sep 22 '22 21:09

Jose


From this answer,

window.location.href not working

you just need to add

return false; 

at the bottom of your function

like image 31
Sruit A.Suk Avatar answered Sep 23 '22 21:09

Sruit A.Suk