Total members 11895 |It is currently Sun Dec 22, 2024 7:49 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





Cookies creation and remove using JQuery

Create Cookie :
javascript code
$.cookie("CookieName", "Value");

Cookie creation with expiry after 1 day:
javascript code
$.cookie("CookieName", "Value", { expires: 1 });

Adding path to the opitions:
javascript code
$.cookie("CookieName", "Value", { expires: 1,path    : '/' });

Adding domain name:
javascript code
$.cookie("CookieName", "Value", { expires: 1,path    : '/',domain  : 'codemiles.com' });

Secure the cookie :

javascript code
$.cookie("CookieName", "Value", { expires: 1,path    : '/',domain  : 'codemiles.com',secure  : true });


To print the content of cookie :
javascript code
alert($.cookie("CookieName"));


To delete the cookie :
javascript code
$.cookie("CookieName", null);

or by :
javascript code
$.removeCookie("test");


Example of using Cookie with JQuery
javascript code
<html>
<head>
<title>Create Cookie- Remove Cookie JQuery</title>

<!--
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">
</script>
Note you can download the JQuery package instead of using Google version. -->
<script src="jquery-1.8.3.js"></script>
<script src="jquery.cookie.js"></script>
<style type="text/css">
body
{
background:#5477BE;
font-size:22px;
}

</style>

<script>
$(document).ready(function(){
$("input#sbtButton").click(function(){

$.cookie("firstCookie", "Yam");

$('div#div1').append("Cookie Created, expires after one day<br/>");
$('div#div1').append("Cookie value="+$.cookie("firstCookie")+"<br/>");
$.cookie("firstCookie", null);
$('div#div1').append("Cookie has been deleted..."+"<br/>");

});
});
</script>
</script>
</head>
<body>


<b>Press on the next button to print and create Cookie :</b><br/>

<input type="button" value="Add Cookie" id="sbtButton" />


<div id="div1"></div>
</body>
</html>


Please note that you have to download the JS files that support the Cookies functions :
Code:
https://github.com/carhartl/jquery-cookie/blob/master/jquery.cookie.js
http://archive.plugins.jquery.com/project/Cookie




_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 1 post ] 

  Related Posts  to : Handle Cookies using JQuery
 Using cookies in ASP     -  
 cookies &rewriting URL     -  
 What are exceptions and how to handle them!!!     -  
 How can a GUI component handle its own events     -  
 Handle IsPostBack Condition     -  
 handle integer overflows and underflows     -  
 Handle Focus events with FocusListener     -  
 Handle Key event-keyboard-Get typed Character and its code     -  
 JQuery Animations     -  
 slide down using JQuery     -  









Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com