window.location.href不适用于Chrome

问题描述 投票:1回答:5

此代码适用于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>
jquery google-chrome window.location
5个回答
1
投票

尝试使用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;
    }
});

2
投票

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

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

作为一个简单的例子:

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

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

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


2
投票

我有同样的问题,我发现两个解决它。第一种方法是设置超时帧,所以代码如下:

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

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

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

希望能帮助到你。


0
投票

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

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


-3
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.