How can I upgrade my JQuery that will save last function when Post or Refresh page?

夙愿已清 提交于 2019-12-11 06:44:01

问题


With help of "@Dimitar Dimitrov" I have create this javascript that changes values after click on link. Here is JSFiddle link: http://jsfiddle.net/3uqF2/19/

Here is code of function:

$(document).ready(function () {
    $(".my-link").on("click", function() { 
        $(".status").hide();
        var linkId = $(this).data("id");
        $("." + linkId).show();
    });
});

My Question is, how can I upgrade it, so when I refresh page or click on post (e.g. search button), my value that I click (e.g. "Eng", watch link of Fiddle) saves, so when I again click on refresh or post, this link would stay until I click another one. Now when I click refresh or post it always return to first link "Slo" (watch link of Fiddle).

!Note: I have put this code in my _Layout.cshtml so I can use it on all pages.

Thanks for help.


回答1:


try saving it in a cookie :

   $(document).ready(function () {
//getting the value of the cookie
if(document.cookie!=""){
     $(".status").hide();
    var linkId = document.cookie;
    $("." + linkId).show();
}
$(".my-link").on("click", function() { 
    $(".status").hide();
    var linkId = $(this).data("id");
 //setting the value of the cookie
    document.cookie=linkId;
    $("." + linkId).show();
});
});



回答2:


You can perform an action when the user is leaving the page by binding a remove call on <body/>:

$('body').bind('remove', function(event) {
    $.get('/api/leaving_page');
});


来源:https://stackoverflow.com/questions/24927981/how-can-i-upgrade-my-jquery-that-will-save-last-function-when-post-or-refresh-pa

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!