ExtJS 4 Cookies

Hello, I thought I should share this one with you. Generally we set cookies using our server side scripts such as in PHP we use set_cookie and $_COOKIE array to set and get cookies data respectively. But if you are developing a client side application and are using ExtJS then this post gives you a kick start to work with cookies using one of our favourite Javascript framework. Lets get started. There are 3 main function with cookies Create a cookie Get a cookie value Remove a cookie We will go through each one of them with example. Create a cookie Alright so lets create a cookie first Here is a simple code Example using Ext.util.Cookies object [crayon-5e56492ddaee0671685655/] The above code creates a cookie with key myCookie. Details of set can be found here http://docs.sencha.com/ext-js/4-1/#!/api/Ext.util.Cookies-method-set Ext.util.Cookies.set() accepts other arguments aswell as shown below set( name, value, [expires], [path], [domain], [secure] ) Parameters name : The name of the cookie to set. value : The value to set for the cookie. expires : Date this cookie will expire. Default is browser session and please note that whatever value you pass here will be converted to GMT data path : Default value is "/" but you can be more restrictive. domain : If you would like to limit the cookie access by domain then you should pass this value. e.g. if I set this value to jaspreetchahal.org then my cookie will be available to anysubdomain.jaspreetchahal.org secure : true or false if you are using a secure channel i.e. request go through HTTPs then set it to true. Please note that cookie will not be available to plain httpequivalent page. You must create a secure cookie while you are using HTTPs protocol. Create a ExtJS 4 cookie with expiry Lets now create another cookie that expires in 1 year time and that is done as shown below [crayon-5e56492ddaeea686646372/] Easy done! Alright now … [Read more...]