您不一定需要使用cookie;您可以嘗試使用store.js來存儲數據,該數據封裝了瀏覽器本地存儲以及其他一些方法來在客戶端存儲數據。
/*
store.js groups your values into something called a store. Multiple stores are separated from each other.
So let's make a new store:
*/
var settings = new Store("settings");
/*
Just choose a name for the new store, and save it in a variable. Always remember to use the "new" keyword! Never leave it off!
Now you can almost normally get, set and remove values:
*/
settings.set("color", "blue");
settings.set("enable_test1", true);
settings.set("number_of_rainbows", 8);
// and
var color = settings.get("color");
// and
settings.remove("color");
...編輯與代碼..
<div id="cookiemsg"><div id="cookiecenter"><p>This website places a
Google Analytics cookie on your machine, this helps us collect
anonymous information so that we can provide a better experiance for
you. By using this site you imply your consent to this. For more
information or to find out how you can remove this cookie please visit
our privacy policy <a href="#">HERE</a> or if you are happy with this
click <a id="hide" href="#">HERE</a></p></div><!--end of
cookiecenter--></div><!--end of cookiemsg-->
$(function(){
var store = new Store("com.domain.page.store")
var acceptedCookie = store.get("acceptedCookie");
if(typeof acceptedCookie == "undefined"){
//set a default
acceptedCookie = false
}
if(!acceptedCookie){
$('#cookiemsg').slideDown('slow');
}
$('#hide').click(function(){
$('#cookiemsg').slideUp('slow');
store.set("acceptedCookie", true);
});
});
好吧,那麼如何實施此解決方案,必須有一個簡單的方法,一次用戶已經點擊了這裏的鏈接與id隱藏它記得它 –
恐怕我的JavaScript很差,但我發現我的自我在我需要使用它的情況 –
我在這裏爲您創建了一個簡單的jsFiddle http://jsfiddle.net/sQEtq/ –