Firefox redirect and javascript

I am currently having a problem with firefox, where all other browsers behave correctly - even IE6!

I want to redirect to a subpage, but leave a history record. As far as I know, there are 2 methods for rewriting URLs:

  • window.location = "some.url"; - redirect to some.url with history record
  • window.location.replace ("some.url"); - redirection without entering history

So, I have to use the first one and test in the firebug console ever working perfectly.

Now there is the weird part of this question: the same statement that worked perfectly on the console, not in some jQuery callback handler:

jQuery("#selector").bind("submit", function() { $.getJSON("some_cool_json", function(response) { var redirect_path = response.path; window.location = redirect_path; }); return false; }); 

where response_path set correctly, I checked it! Even the redirection works correctly, but a history record is not created.

Any ideas on this? It would be great!;)

Greetings

Joe

+4
source share
2 answers

If this happened to me, then I would try to do this:

 jQuery("#selector").bind("submit", function() { $.getJSON("some_cool_json", function(response) { var redirect_path = response.path; setTimeout(function() { window.location.assign(redirect_path); }, 1); }); return false; }); 

The idea is to make the "assign ()" call to the "normal" event handler if there is something about the context of the getJSON response function, which is weird. This function (response "getJSON") is called from the context of the browser executing the code of the <script> block that has just been added to the DOM, so it is at least a little unusual.

I do not know that this will work; I have not tried setting up a test page.

+3
source

use assign () :

 window.location.assign("http://..."); 

replace (URL)
Replace the current document with one at the provided URL. The difference from the assign () method is that after using replace () the current page will not be saved in the history session , that is, the user will not be able to use the back button to navigate to it.

+7
source

Source: https://habr.com/ru/post/1303450/


All Articles