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