0

I have category browsing page initially shows 48 products, and after customers click a next button, it triggers ajax call to load next 48 products, so then the page has 96 products and the url keeps the same. And then, customers click one product from the page, and go to the product page, and then they click back in the browse going back to the browsing page, and the page is reloading and 24 products are shown.

So my question is that how to keep the last ajax state, so when customers click back, they still see 48 results.

I see LLBean can do it, but wondering how to implement this? http://www.llbean.com/llb/shop/509723?nav=ln-26&page=active-clothing

Thanks!

4

1 回答 1

0

是的,您可以使用客户端的 cookie 存储或服务器端的会话数据来执行此操作。我想我会推荐 cookie 存储,因为这是非常基本的不敏感数据。

您可以使用以下代码来写入、读取和擦除 cookie 数据:

function createCookie(name, value, days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    } else var expires = "";
    document.cookie = escape(name) + "=" + escape(value) + expires + "; path=/";
}

function readCookie(name) {
    var nameEQ = escape(name) + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return unescape(c.substring(nameEQ.length, c.length));
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name, "", -1);
}

例如,您可以设置一个名为的 cookienumResults来存储客户端正在浏览的结果数量:

var data = readCookie("numResults");
if (data != null && data != "") {
    //in this case, you have your data, load this many results
} else {
    createCookie("numResults", 48, 30); //create the cookie, if it doesn't already exist
}

当您进行 ajax 调用以增加结果数时,只需添加:

createCookie("numResults", newNumber, 30);
于 2013-04-06T19:25:00.713 回答