Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 568
Tom Anderson Avatar asked Jul 23 '09 19:07

Tom Anderson


People also ask

Which method is used to redirect the webpage in JavaScript?

To redirect to a new URL or page, you assign the new URL to the location. href property or use the location. assign() method.

How do you redirect someone in JavaScript?

Answer: Use the JavaScript window. location Property If you want to redirect the user from one page to another automatically, you can use the syntax window. location. replace("page_url") .

What are JavaScript redirects?

A JavaScript redirect is a piece of JavaScript code that is used to automatically transfer a visitor from a landing page to a different target page.


2 Answers

I know this is an older question, but if you're navigating to a link on the same page, you should be able to simply do the following, where 123 is the item id:

window.location = "items/123";
like image 84
frodo2975 Avatar answered Oct 06 '22 01:10

frodo2975


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();
like image 25
Josh Stodola Avatar answered Oct 05 '22 23:10

Josh Stodola