An instance would last the pop page of Google. This search engine allows users to pick out how many search results they desire to meet on each page. Thanks to a cookie , this configuration remains unchanged for each estimator , fifty-fifty later rebooting several sessions. Despite this , it is skillful to take away them from fourth dimension to fourth dimension because although about volition expire existence erased automatically later a spell , about of them volition never practise so.
Like many other things , cookies may endure a hapless usage in addition to thus , their bad reputation. For instance , about websites tin ship away shop in addition to report the browsing habits of a user without their knowledge. This is why most browsers already include a organisation to filter them in addition to tin ship away determine if they volition render about privacy or not.
If yous desire to meet a alive instance of how a cookie plant , delight catch this page in addition to refresh it a few times. You should teach an warning window telling yous close how many times yous visited the page.
Script source: javascriptkit.com
There could last other ways to usage cookies but a server could practise this amongst JavaScript. Below nosotros convey a elementary script that tin ship away last used every bit many times nosotros desire , fifty-fifty for dissimilar purposes. Basically , what the next script does is to practise 3 functions: the kickoff 1 is to laid a cookie , the 2nd 1 is to read it , in addition to the concluding 1 is to erase it. We tin ship away apply it inward Blogger past times accessing the template HTML in addition to add together it only earlier the tag:
Once done , inward companionship to laid a cookie , nosotros entirely require to set the mention in addition to the value inward quotes when nosotros telephone telephone the function. Additionally , nosotros volition laid the croak appointment past times getting the electrical current Time (in milliseconds) in addition to add together the required give away of minutes (in milliseconds):
var expiration = novel Date();
expiration.setTime(expiration.getTime() + 10000); //Expire later 10 seconds
setCookie(“cookiename” ,”hello” ,expiration);
}
The inward a higher identify code sets a cookie called cookiename , amongst the hello value in addition to laid its croak appointment to 10 seconds later it has been laid (10000 milliseconds = 10 seconds). If nosotros desire to restore the value of this cookie , so nosotros should usage the 2nd role amongst the cookie name:
var checkCookie = getCookie(“cookiename“);
By adding this code below the taag , nosotros created a cookie amongst the value ‘hello‘ which tin ship away last shown on the covert if nosotros desire to. The cookie volition disappear later 10 seconds:
The erase role is used inward the same agency every bit the 1 for reading only past times clicking on the mention of the cookie. The setCookie values for ‘domain’ in addition to ‘secure’ are non used. Use ‘domain’ on the JavaScript cookie if yous are using it on a subdomain , where the cookie is onslaught the widgets subdomain , but yous require it to last accessible over the whole yoursite.com domain.
The combination of these 3 functions volition allow us to grip cookies for specific purposes , every bit nosotros volition meet inward the close future.