I'm trying to make both back and forward buttons for my website. Actually I want to follow this. So first of all, I need to check whether do window.history.back();
and window.history.forward();
exist? if yes, then show their buttons and set a href
attribute for them.
function creat_back_forward_btn(){
if ( window.history.back() ) {
$('.back_btn').show(); // in first, the button is hidden (by CSS)
var previous_page = window.history.back();
$('.back_btn').attr('href', previous_page );
}
if ( window.history.forward() ) {
$('.forward_btn').show(); // in first, the button is hidden (by CSS)
var next_page = window.history.forward();
$('.forward_btn').attr('href', next_page );
}
}
creat_back_forward_btn(); // on load
And then, I will use those buttons like this:
$('.back_btn, .forward_btn').on('click', function() {
var page = $(this).attr('href');
location.href=page
});
But my code doesn't work as expected, I mean I even cannot open my website. It will be redirected somewhere without clicking on anything. How can I fix it?