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?
Window Location Href The window.location.href property returns the URL of the current page.
window. location. href method returns/loads the current url. So we can use it to reload/refresh the page in javascript.
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.
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.
From this answer,
window.location.href not working
you just need to add
return false;
at the bottom of your function
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With