即使在Chrome上进行URL重定向后,Jquery仍继续运行

时间:2011-12-14 03:23:56

标签: jquery google-chrome redirect

$('#target').click(function() {
window.location.replace("http://www.google.com");
return false;
}); 

如何告诉chrome和所有其他浏览器停止他们正在做的事情并立即重定向到另一个网页?

Firefox 8.0会立即执行window.location.replace,正如我所希望的那样。

当我单步执行上面的代码时,Chrome 15.0.874.121会飞过window.location代码并返回false,然后进入后续的Jquery函数(换句话说,Chrome会完成整个调用堆栈,然后替换URL )

我正在尝试在许多不同的区域执行重定向,最大的问题是额外的Jquery可以为重定向添加3-5秒的时间,尤其是在.ajax成功调用中。

我正在使用jQuery 1.7.1并且没有javascript错误

我试过了:

  • 将'return false'放在任何地方
  • 使用不同的重定向(location.reload(true),location.href等)
  • 将我的重定向移到任何其他功能之外
  • 将我的重定向完全移至单独的功能

无论我做什么,Chrome都拒绝直接转到其他网站而不先完成其调用堆栈。请帮忙。

4 个答案:

答案 0 :(得分:1)

$('#target').click(function() {
window.location.replace("http://www.google.com");
//THROW an exception is the only solution I've found so far
throw "Reloading Page";
}); 

不幸的是,我没有找到更好的答案。毕竟这不是一个jquery问题。我已经删除了所有内容:删除了chrome中的所有扩展名,删除了所有外部文件,使用最少的javascript和简单的onclick创建了一个完全空白的.html文件......并且Chrome STILL通过重定向进行了轰炸。

所以:截至目前,我唯一的答案是抛出一个例外,告诉Chrome在重定向后立即停止运行JS。

感谢Gijs

编辑:不幸的是,Chrome会暂停抛出异常,因此事实证明这不是一个可部署的解决方案。

答案 1 :(得分:0)

为什么不使用简单的Javascript而只使用

document.location = "http://google.com";

答案 2 :(得分:0)

您是否尝试过event.preventDefault

$('#target').click(function(evt) {
    evt.preventDefault();
    window.location.replace("http://www.google.com");
    return false;
}); 

答案 3 :(得分:0)

我遇到了完全相同的问题。我最终使用了if else块并且它有效。

的CoffeeScript

if Number(deck.length) == Number(deck.counter)
     window.location.href = "/rounds" 
else
    whatever...

的Javascript

if (Number(deck.length) === Number(deck.counter)) {
      return window.location.href = "/rounds";
    } else {
相关问题