jQuery window.location.href 不适用于 Chrome

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18138753/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 21:05:18  来源:igfitidea点击:

window.location.href not working for Chrome

jquerygoogle-chromewindow.location

提问by Mathew Pregasen

This code works for Firefox but not for Chrome. I believe it is the window.location.href but what can I do to have this work for chrome. Basically this switches pages.

此代码适用于 Firefox,但不适用于 Chrome。我相信它是 window.location.href 但我该怎么做才能让这项工作适用于 chrome。基本上这会切换页面。

<!DOCTYPE html>
<html>
 <head>
    <script src="http://code.jquery.com/jquery-1.6.4.js"></script>
<script>
$(document).keydown(function(e){    
    if (e.keyCode == 37) {  // left
       window.location.href = $('#prev').attr('href');   
       return false;
    } else if (e.keyCode == 39) {  // right
       window.location.href =  $('#next').attr('href');  
       return false;
    }
});


</script>


   </head>
 <body>


  <div style="display:hidden;">
    <a id="next" href=<?php echo "readerapp.php?mode=$mode&pagenumber=$next";?>></a>
    <a id="prev" href=<?php echo "readerapp.php?mode=$mode&pagenumber=$last";?>></a>
</div>
   </body>
</html>

回答by aleksblago

The paths you're sending to the browser are relative. You'll need to supply JS with the full URL. Try this:

您发送到浏览器的路径是相对的。您需要向 JS 提供完整的 URL。尝试这个:

window.location.href = 'http://'+ window.location.host + $('#next').attr('href');

As a quick example:

举个简单的例子:

On this page, open up the console in chrome (Shift+CTRL+j) and type in:

在此页面上,在 chrome ( Shift+ CTRL+ j) 中打开控制台并输入:

window.location.href = 'http://' + window.location.host + $('.bottom-notice a:last').attr('href');

It'll redirect you to https://stackoverflow.com/questions/ask

它会将您重定向到https://stackoverflow.com/questions/ask

回答by Hooman

I had the same problem and I found two work around for it. the first method is to set a timeout frame, so the code would be like this:

我遇到了同样的问题,我找到了两个解决方法。第一种方法是设置一个超时帧,所以代码是这样的:

setTimeout(function () { document.location.href = "nextpage.html" }, 1000);

the second solution is to assign the next page to window.location and it would be as follow:

第二种解决方案是将下一页分配给 window.location ,如下所示:

window.location.assign("nextpage.html");

Hope it helps.

希望能帮助到你。

回答by Rejayi CS

Try it with window.location:

试试看window.location

$(document).keydown(function(e){
    if (e.keyCode == 37) {  // left
        window.location = $('#prev').attr('href');   
        return false;
    } else if (e.keyCode == 39) {  // right
        window.location =  $('#next').attr('href');  
        return false;
    }
});

回答by Guest Gerard

Clicking an input type="submit"does not allow a script to set the location.href

单击input type="submit"不允许脚本设置location.href

An input type="button"works fine.

一个input type="button"工作正常。

回答by Kiran Ramaswamy

So I know this is a bit of an old thread, but since it's the #1 result on google when searching, figured I'd add my two cents here.

所以我知道这是一个旧线程,但由于它是搜索时在谷歌上排名第一的结果,我想我会在这里加上我的两分钱。

For me, the problem ended up being that I was doing this in a fancyBox, and calling "parent.jQuery.fancybox.getInstance().close();" right after. In IE, this worked fine, but I guess in Chrome, by calling the .close(), it was preventing the redirect from working.

对我来说,问题最终是我在一个fancyBox 中执行此操作,并调用“parent.jQuery.fancybox.getInstance().close();” 紧随其后。在 IE 中,这工作正常,但我猜在 Chrome 中,通过调用 .close(),它阻止了重定向工作。

The solution was to just remove that line. The window.location.href forced it to close anyway, since the main window redirected.

解决方案是删除该行。window.location.href 无论如何都强迫它关闭,因为主窗口重定向了。

回答by avatoor

After restarting Chrome, the problem went away for me.

重新启动 Chrome 后,问题就消失了。