[英]jquery bbq - detect when user clicks back button
I've successfully implemented the jQuery BBQ plugin to build a quick prototype, however I am having one small issue related to my particular setup: 我已经成功实现了jQuery BBQ插件来构建一个快速原型,但是我有一个与我的特定设置相关的小问题:
Is there a way to know that the user has hit the back button, so in that case I don't trigger the "randomItems()" function? 有没有办法知道用户已经按下后退按钮,所以在这种情况下我不会触发“randomItems()”函数?
You need to put the items from randomItems()
in a cache: 您需要将来自
randomItems()
的项目放在缓存中:
// save your records here in the cache
var items = [];
$(window).on('hashchange', function(){
var page = window.location.hash.replace(/^#/, '');
// check first if items has already records in it and check if page is #catalogue
// if not then call randomItems()
if (page === 'catalogue' && items.length === 0) {
items = randomItems();
}
// your code here follows
});
use the browser's local storage to save the item. 使用浏览器的本地存储来保存项目。 first create a javascript representation of the object.
首先创建对象的javascript表示。 Then store it in the localStorage.
然后将其存储在localStorage中。 Retrieve in on page load.
检索页面加载。 Note that you will have to stringify the complex object to store it because the storage only accepts key value pairs
请注意,您必须对复杂对象进行字符串化以存储它,因为存储只接受键值对
heres a general pattern 这是一种普遍的模式
var foo;
window.onpageload = function(){
if(localStorage["foo"] == '' | localStorage["foo"] == undefined){
foo = getrandomItems(); //possibly an ajax call;
localStorage["foo"] = JSON.stringify(foo);
else{
foo = JSON.parse(localStorage["foo"]);
}
};
see http://diveintohtml5.info/storage.html and Storing Objects in HTML5 localStorage for details 有关详细信息,请参阅http://diveintohtml5.info/storage.html并在HTML5 localStorage中存储对象
Believe bbq allows you to set and get url parameters, you can use these to determine if you need to randomise. 相信bbq允许你设置和获取url参数,你可以用这些来确定你是否需要随机。 When the user clicks link to catalog the url is something like:
当用户点击链接到目录时,网址就像:
http://yoururl.com/#catalog
So this is the "root" url for catalog and whenever the url is this you randomise Items, after randomising the items you add a param to the url so it becomes say: 所以这是目录的“根”网址,每当网址是你随机化的项目,随机化项目后你将一个参数添加到网址,所以它变成了:
http://yoururl.com/#catalog?r=1
This way when the user goes off and looks at an item and then clicks back historic url will contain the r=1
and as such you do not process the randomise function. 这样,当用户关闭并查看项目然后单击返回历史URL时,将包含
r=1
,因此您不会处理随机函数。
function randomiseItems()
{
// we do not want to randomise
if(urlParamIsSet("r")) return;
// set url param so gets stored in history (replacing current url)
urlSetParam("r",1);
//-- do your code here
}
Instead of urlParamIsSet/urlSetParam
use whatever function bbq provides for you to manipulate url. 而不是
urlParamIsSet/urlSetParam
使用bbq提供的任何函数来操作url。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.