firefox and javascript redirection

余生颓废 提交于 2020-05-11 10:58:46

问题


I currently have a issue with firefox, where all other browser behave in the right way - even IE6!

What I want to do is redirection to a subpage but leaving a history entry. There are 2 methods of rewriting the url as far as I know:

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

So I have to use the first one and tested in the firebug console everthing works fine.

Now there is the kind of strange part of this question: the same statement, that worked fine in the console doesn't 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 is set correctly, I checked it! Even the redirection is working correctly, but there is no history entry created.

Any ideas on that one? Would be great! ;)

Cheers

Joe


回答1:


If this were happening to me, a thing that I'd try would be 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 put the execution of the "assign()" call into a "normal" event handler, in case there's something about the context of the "getJSON" response function that's weird. That function (the "getJSON" response) will be called from the context of the browser executing the code of a <script> block that's just been added to the DOM, so it's at least a little bit unusual.

I don't know that it'll work; I haven't tried setting up a test page.




回答2:


use assign():

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

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



来源:https://stackoverflow.com/questions/2401165/firefox-and-javascript-redirection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!