当前位置: 动力学知识库 > 问答 > 编程问答 >

How to remember last ajax state when click back in browse

问题描述:

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!

网友答案:

Yes you can do this with either cookie storage on client side, or session data on server side. I guess I would recommend cookie storage since this is very basic insensitive data.

You can use the following code to write, read and erase cookie data:

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);
}

For example, you could set a cookie named numResults to store how many results the client is browsing:

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
}

And when you make the ajax call to increase results number, just add:

createCookie("numResults", newNumber, 30);
分享给朋友:
您可能感兴趣的文章:
随机阅读: