Abudayah Abudayah - 7 months ago 9
Javascript Question

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>

Answer

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

});