开发者

firefox and javascript redirection

开发者 https://www.devze.com 2022-12-22 01:33 出处:网络
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 re

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,开发者_StackOverflow 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


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消