RGR
RGR

Reputation: 1571

Event before loading ajax content

I am trying to maintain few objects in cookie to restore some settings. Initially i tried in window.unload to save and restore settings using cookie.It worked in full page load. But it didn't work in my case as whole page is not loading just replacing the specific content using ajax post(ajax load). So can you tell me in which event can i save those values in cookie to restore back while using ajax post ?

I tried the below code in window.unload event

window.onunload = function (e) {
    var gridObjModel = $("#Grid");
    var myCookie = escape(JSON.stringify({ 
        "CurrentPage": gridObjModel.pageSettings.currentPage, 
        "SortedColumns": gridObjModel.sortSettings.sortedColumns, 
        "GroupedColumns": gridObjModel.groupSettings.groupedColumns 
    }));
    document.cookie = myCookie;
}

Upvotes: 2

Views: 85

Answers (1)

mekwall
mekwall

Reputation: 28974

You could try with the following:

$(window).on("unload", function (e) {
    var gridObjModel = $("#Grid");
    var myCookie = escape(JSON.stringify({ 
        "CurrentPage": gridObjModel.pageSettings.currentPage, 
        "SortedColumns": gridObjModel.sortSettings.sortedColumns, 
        "GroupedColumns": gridObjModel.groupSettings.groupedColumns 
    }));
    document.cookie = myCookie;
});

When you do a ajax load just trigger unload before:

$(window).trigger("unload");
$("#foo").load(someUrl);

Upvotes: 1

Related Questions