window.location.href not working for Chrome

31,802

Solution 1

Try it with 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;
    }
});

Solution 2

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.assign("nextpage.html");

Hope it helps.

Solution 3

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

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:

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

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

Solution 4

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.

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

Share:
31,802
Mathew Pregasen
Author by

Mathew Pregasen

Updated on November 04, 2020

Comments

  • Mathew Pregasen
    Mathew Pregasen over 3 years

    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.

    <!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>
    
  • MorkPork
    MorkPork over 10 years
    I was getting this issue and adding in the window.location.host did the trick for me
  • Green
    Green almost 8 years
    Difference? Why do you this this should work? What about href?
  • T3rm1
    T3rm1 almost 7 years
    This is the only solution that worked for me on all browsers (even mobile).
  • sharun k k
    sharun k k over 3 years
    document.location.href = "nextpage.html" is not working but when i add the timout it works.