简体   繁体   English

如何使用带有jQuery和自定义url参数的cookie

[英]How to use cookies with jQuery and custom url parameters

I have three buttons that add parameters to my url - something like this: 我有三个将参数添加到url的按钮-类似于:

<a class="click1">/a> = ?param=grid
<a class="click1">/a> = ?param=smallgrid
<a class="click1">/a> = ?param=largegrid

These buttons shows three different layout - the first one is set as default. 这些按钮显示三种不同的布局-第一种设置为默认布局。

I want to place the users choice in a cookie - but I need the url only to be added to the relevant pages. 我想将用户的选择放在cookie中-但是我只需要将URL添加到相关页面即可。

The url looks like this: 网址看起来像这样:

 /products/Computers/Notebooks?param=list

so I want the cookie to execute the choice based on the url has /products or even better - a body class if possible. 因此,我希望Cookie根据URL具有/ products甚至更好的属性执行选择-如果可能的话,使用body类。

I have added the jquery.cookie.js plugin to my site - but i cant figure out how to use it. 我已将jquery.cookie.js插件添加到我的网站-但我不知道如何使用它。

This SO answer shows a very basic usage of the JQuery Cookie library. 这样的答案显示了JQuery Cookie库的非常基本的用法。 Basically the usage is $.cookie(<cookie-key>, <cookie-value>, {<additional data about cookie>}) . 基本上,用法是$.cookie(<cookie-key>, <cookie-value>, {<additional data about cookie>}) (This is obviously pseudocode). (这显然是伪代码)。 This will write a key/value cookie to the browser, which will last as long as you specify and can be fetched via $.cookie("<cookie-key>") and deleted via $.removeCookie("<cookie-key>") . 这会将键/值cookie写入浏览器,该cookie会一直持续到您指定的时间,并且可以通过$.cookie("<cookie-key>")获取,并可以通过$.removeCookie("<cookie-key>")

So for your use case, it might look like this: 因此,对于您的用例,它可能看起来像这样:

HTML
<a id="gridbtn" class="click1"></a>
<a id="smallgridbtn" class="click1"></a>
<a id="largegridbtn" class="click1"></a>

// JQuery
$(document).ready(function() {
    // After page load
    var cookieLayoutValue = $.cookie("layout");

    console.log("The cookie value was " + cookieLayoutValue);

    if (cookieLayoutValue === "grid") {
        // process "grid" layout
    } else if (cookieLayoutValue === "smallgrid") {
        // process "smallgrid" layout
    } else if (cookieLayoutValue === "largegrid") {
        // process "largegrid" layout
    } else {
        // cookie doesn't exist, default it to grid
        $.cookie("layout", "grid", { expires : 999 });
       // process "grid" layout
    }

    //
    // call layout code for whatever the value is using if/else
    //

    // Wire up some click handlers
    $("#gridbtn").click(function() {
        var layout = $.cookie("layout");
        $.cookie("layout", "grid", { expires : 999 });

        // If the layout has changed
        if (layout !== "grid") {
            // Do "grid" layout processing
        }            
    });

    // Wire up some click handlers
    $("#smallgridbtn").click(function() {
        var layout = $.cookie("layout");
        $.cookie("layout", "smallgrid", { expires : 999 });

        // If the layout has changed
        if (layout !== "smallgrid") {
            // Do "smallgrid" layout processing
        }            
    });

    // Wire up some click handlers
    $("#largegridbtn").click(function() {
        var layout = $.cookie("layout");
        $.cookie("layout", "largegrid", { expires : 999 });

        // If the layout has changed
        if (layout !== "largegrid") {
            // Do "largegrid" layout processing
        }            
    });
});

Otherwise, you'd have to send the information you want in the cookie to the server for processing. 否则,您必须将cookie中所需的信息发送到服务器进行处理。 I'd recommend a RESTful service via Spring Framework and then set the cookie in the response like this . 我建议通过Spring Framework提供一个RESTful服务,然后像这样在响应中设置cookie。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM