简体   繁体   English

window.location.replace() 无法重定向浏览器

[英]window.location.replace() not working to redirect browser

I make navigation with pages but this code not work, what's the problem ?我使用页面进行导航,但此代码不起作用,有什么问题?

<script>
$(document).ready(function() {
$("body").keydown(function(event) {
  if(event.keyCode == 37) { // left
    window.location.replace("http://newsii.abudayah.com/photo/2)";  }
  else if(event.keyCode == 39) { // right
    window.location.replace("http://newsii.abudayah.com/photo/31)";  }
});
});
</script>

Don't use .replace() for this, just assign the value directly.不要为此使用.replace() ,只需直接分配值。

Example例子

$("body").keydown(function(event) {

    if(event.keyCode == 37) { // left
        window.location = "http://newsii.abudayah.com/photo/2";
    }
    else if(event.keyCode == 39) { // right
        window.location = "http://newsii.abudayah.com/photo/31"; 
    }

});

Your code has a syntax error.您的代码有语法错误。 Your end parenthesis is inside the quote not outside...您的结尾括号在引号内而不是在外...

Try:尝试:

<script>
$(document).ready(function() {
$("body").keydown(function(event) {
  if(event.keyCode == 37) { // left
    window.location.replace("http://newsii.abudayah.com/photo/2");  }  
  else if(event.keyCode == 39) { // right
    window.location.replace("http://newsii.abudayah.com/photo/31");  }
});
});
</script>

window.location.replace is not supported in all browsers.并非所有浏览器都支持 window.location.replace。 Assigning the location value is always supported.始终支持分配位置值。 However, the reason to use replace rather than assigning the location value is you don't want the current url to appear in the history, or to show-up when using the back button.但是,使用替换而不是分配位置值的原因是您不希望当前 url 出现在历史记录中,或者在使用后退按钮时出现。 Since this is not always possible, you just need to settle for what is possible:由于这并不总是可能的,您只需要满足于可能的情况:

<script>
$(document).ready(function() {
$("body").keydown(function(event) {
  if(event.keyCode == 37) { // left
    try { window.location.replace("http://newsii.abudayah.com/photo/2"); } 
    catch(e) { window.location = "http://newsii.abudayah.com/photo/2"; }
  }
  else if(event.keyCode == 39) { // right
    try { window.location.replace("http://newsii.abudayah.com/photo/31"); } 
    catch(e) { window.location = "http://newsii.abudayah.com/photo/31"; }
  }
});
});
</script>

I had an issue with it not working when reloading same page in Chrome .Chrome 中重新加载同一页面时,我遇到了它无法工作的问题。 Doing the following worked:做以下工作:

   window.location.replace("/mypage1.aspx?type=abc"); //redirect to fake page
   window.location.replace("/mypage.aspx?type=abc");  //redirect to same page

It is a bit of a hack, but this seems to be the only thing that forces a reload on the same page in Chrome.这有点骇人听闻,但这似乎是在 Chrome 中强制重新加载同一页面的唯一方法。 IE and FF work without the redirect to a fake page. IE 和 FF 无需重定向到假页面即可工作。

I was having trouble with this in Chrome.我在 Chrome 中遇到了这个问题。 I was trying to load another page from the same domain, but was using an absolute URL (eg www.example.com/newurl ).我试图从同一个域加载另一个页面,但使用的是绝对 URL(例如www.example.com/newurl )。 I changed it to a relative URL ( /newurl ) and it works now.我将其更改为相对 URL ( /newurl ),现在可以使用了。

My thought is that this is a security feature to prevent the user from being redirected to a malicious site through some javascript ad.我的想法是,这是一项安全功能,可防止用户通过某些 javascript 广告被重定向到恶意站点。

I used this and it's work我用了这个,它的工作

$(document).ready(function () {

    $(document).keydown(function(e) {
        var url = false;
        if (e.which == 37) {  // Left arrow key code
            url = $('.prev').attr('href');
        }
        else if (e.which == 39) {  // Right arrow key code
            url = $('.next').attr('href');
        }
        if (url) {
            window.location = url;
        }
    });

});

Use this way:使用这种方式:

window.history.pushState("", "", "new url");
window.location.reload();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM