简体   繁体   English

如何创建后退/前进按钮?

[英]How can I create a back/forward button?

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(); 因此,首先,我需要检查window.history.back(); and window.history.forward(); window.history.forward(); exist? 存在? if yes, then show their buttons and set a href attribute for them. 如果是,则显示其按钮并为其设置href属性。

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? 我该如何解决?

Try This: 尝试这个:

function creat_back_forward_btn(){
    if ( window.history.length > 0 ) {
        $('.back_btn').show().on("click", function(){
            window.history.back();
        }); 

        $('.forward_btn').show().on("click", function(){
            window.history.forward();
        });
    } 
}

creat_back_forward_btn();  // on load

To check whether the function exists, you are using: 要检查该功能是否存在,请使用:

if (window.history.back())

This executes the method. 这将执行该方法。 You should instead use: 您应该改用:

if (window.history.back)

which returns a true value if it is available. 如果可用,则返回一个真值。

Finally, you need to create a function or anonymous function that calls the method and bind that to the href attribute, instead of executing the method and assigning the return value to a variable. 最后,您需要创建一个调用该方法并将其绑定到href属性的函数或匿名函数,而不是执行该方法并将返回值分配给变量。 Like so: 像这样:

var previous_page = function () { window.history.back() };
$('.back_btn').attr('href', previous_page );

Try something like this 试试这个

if ( window.history.back() ) {
    $('.back_btn').show(); // in first, the button is hidden (by CSS)
    var previous_page = window.history.back(-1);
    $('.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(+1);
    $('.forward_btn').attr('href', next_page );    
} 
}

creat_back_forward_btn();  // on load

There are several problems with your code. 您的代码有几个问题。 First one is that, the way you are checking if the there is an function window.history.back() will cause the execution of that function, so the browser goes back in the history immediately. 第一个是,您检查是否有一个函数window.history.back()会导致该函数执行的方式,因此浏览器会立即返回历史记录。 To check for the function presence you can use this code: 要检查功能是否存在,可以使用以下代码:

typeof(window.history.back) === "function"

However, this will only tell if the function is there, not if there is something in the browser history. 但是,这只会告诉您该功能是否存在,而不会告诉您浏览器历史记录中是否包含该功能。 Actually you can not check how many records are in the browser history, because it is an security issue. 实际上,您无法检查浏览器历史记录中有多少条记录,因为这是一个安全问题。 You can handle event popstate together with methods pushState and replaceState to react to the history change. 您可以将事件popstate与方法pushStatereplaceState一起处理以对历史记录做出反应。 However, if you imagine the situation that you open the browser window with some page A , than navigate to your page B , there will be a record in the browser history and you will be able to navigate back to A using browser buttons, but you will not be able to detect in your page B that there is a place to go back. 但是,如果您想象这样的情况:打开带有页面A的浏览器窗口,而不是导航到页面B ,则浏览器历史记录中将有一条记录,您将能够使用浏览器按钮导航回A ,但是将无法在您的页面B中检测到有可以退回的地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM