[removed].replace() not working to redirect browse

2019-01-19 12:20发布

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>

5条回答
Ridiculous、
2楼-- · 2019-01-19 12:30

Don't use .replace() for this, just assign the value directly.

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

});
查看更多
▲ chillily
3楼-- · 2019-01-19 12:33

I had an issue with it not working when reloading same page in 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. IE and FF work without the redirect to a fake page.

查看更多
劳资没心,怎么记你
4楼-- · 2019-01-19 12:49

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.

My thought is that this is a security feature to prevent the user from being redirected to a malicious site through some javascript ad.

查看更多
狗以群分
5楼-- · 2019-01-19 12:54

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:

<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>
查看更多
虎瘦雄心在
6楼-- · 2019-01-19 12:57

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

});
查看更多
登录 后发表回答