我想要做的是在与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;
}
}
}
答案 0 :(得分:0)
我建议你将jquery back button query plugin用于同一目的
示例:
// 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 );