firefox和javascript重定向 [英] firefox and javascript redirection

查看:83
本文介绍了firefox和javascript重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前在firefox上遇到问题,所有其他浏览器的行为都正确,甚至是IE6!

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

我想要做的是重定向到子页面,但保留历史记录条目.据我所知,有两种重写URL的方法:

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"; -使用历史记录条目重定向到some.url
  • window.location.replace("some.url"); -无需输入历史记录即可重定向

所以我必须使用第一个并在Firebug控制台中进行测试,一切正常.

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

现在,这个问题有点奇怪:同一条语句在控制台中运行良好,但在某些jQuery回调处理程序中却没有:

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;
});

正确设置了response_path的地方,我检查了它!即使重定向工作正常,但是也没有创建历史记录条目.

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! ;)

欢呼

推荐答案

如果这发生在我身上,我会尝试做的事情是这样:

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;
});

这个想法是将"assign()"调用的执行放入常规"事件处理程序中,以防"getJSON"响应函数的上下文有些奇怪.该功能("getJSON"响应)将在浏览器的上下文中调用,该上下文执行刚刚添加到DOM中的<script>块的代码,因此至少有点不寻常.

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.

这篇关于firefox和javascript重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆