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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8898998/
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-24 07:59:34  来源:igfitidea点击:

window.location.replace() not working to redirect browser

javascriptjqueryredirectreplacelocation

提问by Abudayah

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>

回答by jondavidjohn

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

});

回答by Bill

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. 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. Since this is not always possible, you just need to settle for what is possible:

并非所有浏览器都支持 window.location.replace。始终支持分配位置值。但是,使用替换而不是分配位置值的原因是您不希望当前 url 出现在历史记录中,或者在使用后退按钮时出现。由于这并不总是可能的,您只需要满足于可能的情况:

<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>

回答by user3413723

I was having trouble with this in Chrome. I was trying to load another page from the same domain, but was using an absolute URL (e.g.www.example.com/newurl). I changed it to a relative URL (/newurl) and it works now.

我在 Chrome 中遇到了这个问题。我试图从同一个域加载另一个页面,但使用的是绝对 URL(例如www.example.com/newurl)。我将其更改为相对 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 广告被重定向到恶意站点。

回答by Ewald Stieger

I had an issue with it not working when reloading same page in Chrome. Doing the following worked:

Chrome 中重新加载同一页面时,我遇到了它无法工作的问题。做以下工作:

   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. IE and FF work without the redirect to a fake page.

这有点骇人听闻,但这似乎是在 Chrome 中强制重新加载同一页面的唯一方法。IE 和 FF 无需重定向到假页面即可工作。

回答by Abudayah

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

});

回答by SeyyedMojtaba

Use this way:

使用这种方式:

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