0

我想要做的是在使用 jQuery 的站点中启用后退和前进按钮。我现在有一个工作代码,但我很确定它太复杂了,我需要一些帮助来使它更容易并添加一个函数来让它继续前进。这是代码:

var recenturl;
var currenturl;

setInterval(function(){
    currenturl = /[^/]*$/.exec(window.location.pathname)[0];

    if(currenturl == 'menu_item1' || currenturl == 'menu_item2' || currenturl == 'menu_item3' || currenturl == 'menu_item4'){
        if(currenturl != recenturl && recenturl != ''){
            console.log(currenturl+" "+recenturl);
            showFrame(currenturl);
            if (typeof(window.history.pushState) == "function"){
                window.history.pushState(null, currenturl, currenturl);
            }
            window.history.back();
            recenturl = currenturl; 
        }
    }      
}, 100);


var xurl = /[^/]*$/.exec(window.location.pathname)[0];

if(xurl =='menu_item1' || xurl == 'menu_item2' || xurl == 'menu_item3' || xurl == 'menu_item4'){
    showFrame(xurl);

if (typeof(window.history.pushState) == "function"){
    window.history.pushState(null, xurl, xurl);
} 
else {
    window.location.hash = "#!" + xurl;
}

}
else {
    if (xurl != " "){
        var defaulturl = "/";       
        showFrame(defaulturl);

        if (typeof(window.history.pushState) == "function"){
            window.history.pushState(null, defaulturl, defaulturl);
        } 
        else {
            window.location.hash = "#!" + defaulturl;
        }

    }
}
4

1 回答 1

0

我建议您出于相同目的使用jquery 后退按钮查询插件

例子:

// Serializing a params string using the built-in jQuery.param method.
// myStr is set to "a=1&b=2&c=true&d=hello+world"
var myStr = $.param({ a:1, b:2, c:true, d:"hello world" });

// Deserialize the params string into an object.
// myObj is set to { a:"1", b:"2", c:"true", d:"hello world" }
var myObj = $.deparam( myStr );

// Deserialize the params string into an object, coercing values.
// myObj is set to { a:1, b:2, c:true, d:"hello world" }
var myObj = $.deparam( myStr, true );

// Deserialize jQuery 1.4-style params strings.
// myObj is set to { a:[1,2], b:{ c:[3], d:4 } }
var myObj = $.deparam( "a[]=1&a[]=2&b[c][]=3&b[d]=4", true );
于 2012-10-15T17:56:31.647 回答