简体   繁体   English

jquery bbq - 检测用户点击后退按钮的时间

[英]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插件来构建一个快速原型,但是我有一个与我的特定设置相关的小问题:

  • One of the pages, "#catalogue" , includes a grid of items that are generated randomly using a function, "randomItems()" , triggered on hashchange (when coming from another page). 其中一个页面“#catalogue”包括一个项目网格,这些项目使用函数“randomItems()”随机生成,在hashchange上触发(来自另一个页面时)。
  • The user then clicks on any of these items in the grid to view the #detailspage . 然后,用户单击网格中的任何这些项目以查看#details页面
  • Upon clicking on the browser Back button, I'd like the user to view the #catalogue page (which works fine) BUT prevent the page from generating a new set of random items onload (so keep whatever items were last shown). 单击浏览器后退按钮,我希望用户查看#catalogue页面(工作正常)但是阻止页面生成一组新的随机项目onload(所以保留上次显示的任何项目)。

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.

相关问题 检测用户是否单击浏览器后退按钮-页面上有jQuery - Detect if the user clicks the Browsers Back Button - Have jQuery on page 在Javascript中,最好是JQuery,当用户点击浏览器中的“后退”按钮时,如何在URL中添加内容? - In Javascript, preferably JQuery, how do I add something to the URL when the user clicks “back” button in the browser? 用户单击浏览器后退按钮时触发事件 - Trigger event when user clicks the browsers back button 当用户单击浏览器的后退按钮时隐藏div - Hide div when user clicks browser's back button 避免在用户单击html按钮时回发 - Avoid posting back when user clicks html button 用户单击浏览器后退按钮时提交表单 - submit form when user clicks browser back button 用户单击浏览器的“后退”按钮时清除文本字段/文本框 - Clearing textfields/textboxes when user clicks the browser's Back button 用户单击后退或前进按钮时运行jQuery ajax函数 - Running jQuery ajax function when user clicks the back or forward buttons 用户单击“后退”按钮时如何进行回发? - How to make a postback when the user clicks the back button? 使用jQuery如何在用户单击单选按钮时更改模型 - using jquery how to change a model when the user clicks a radio button
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM