admin管理员组

文章数量:1291367

I am showing a jQuery overlay when users first visit a particular page

They have a radio button in the overlay that they can click to say they do not wish to see the notification again

I want to set a cookie that the user doesnt want to see the overlay again

Is it possible to set the cookie without refreshing the page?

I was going to make a ajax call back to the server and then set the cookie in the response headers but I guess they wont get set in an Ajax request/response?

Is it safe / OK to set the cookie purely from javascript? Or is that a bad idea?

any other options?

I am showing a jQuery overlay when users first visit a particular page

They have a radio button in the overlay that they can click to say they do not wish to see the notification again

I want to set a cookie that the user doesnt want to see the overlay again

Is it possible to set the cookie without refreshing the page?

I was going to make a ajax call back to the server and then set the cookie in the response headers but I guess they wont get set in an Ajax request/response?

Is it safe / OK to set the cookie purely from javascript? Or is that a bad idea?

any other options?

Share Improve this question edited Apr 15, 2013 at 15:41 ChrisCa asked Apr 15, 2013 at 15:32 ChrisCaChrisCa 11.1k22 gold badges85 silver badges119 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 2

There is no reason that an AJAX call cannot set a cookie. It is basically just an HTTP request.

AJAX Requests Get And Set Cookies Like Any Other HTTP Request

You may use jQuery Cookie Plugin.

Some example:

function setCookieFilters() {
    var $filtersContent = $(".dynamic-filters");
    if ($filtersContent.length > 0) {
        if ($filtersContent.css("display") == "none") {
            $.cookie("isUsingFilters", "true", { expires: 7 });
        }
        else {
            $.cookie("isUsingFilters", "false", { expires: 7 });
        }
    }
}

you could also just get/set the cookie in plain old javascript: Create and Store a Cookie

Use this: this shall reload the page.

setcookie($data,$item_data, time()+ (3600));
echo "<script>location.href='index.php'</script>;

本文标签: javascriptSet cookie without page refreshStack Overflow