Tom Anderson Tom Anderson - 6 months ago 36
HTML Question

What is a good solution for cross browser javascript redirect with history?

I am currently fighting Google Chrome on the following action:

location.href = url





location.replace(url)





document.location = url





window.navigate(url) // doesn't work in Chrome/Firefox





location.assign(url)





window.open(url, '_self')





window.location.href = url





I have tried all, and neither will add a history entry. Is there a way in Google Chrome to do a javascript redirect WITH history?

Thanks.




Explanation
We have a table of items, when clicking on the row, I want the page to navigate to a specified URL, if anyone has a good solution to this other than using the onclick=send method we are using now, please let me know.




Update
It appears that Stackoverflow its-self has this exact same issue. In the main view, click on one of the first 3 columns in the question list (# answers, etc..), then click the back button, it will take you back 2 pages.

Answer

Although, I first must say that this is Chrome behaving stupid, and you probably should not worry about it. Try to create a dummy form and with a GET method and programmatically submit it...

<form id="dummyForm" method="GET" action="#">
  <input type="hidden" name="test" value="test" />
</form>

Then your onclick...

var frm = document.forms["dummyForm"];
frm.action = url;
frm.submit();
Comments