- 浏览: 261659 次
- 性别:
- 来自: 济南
文章分类
最新评论
-
hyxj1220:
原来是这么回事,,谢谢了
document的execCommand解释 -
zouhongmin:
太强了,我膜拜你,要向你学习。
减肥日志 -
codeboy_wang:
超炫 超喜欢
document的execCommand解释 -
laiang8283:
博主男的女的,100斤也不是很胖啊
减肥日志 -
yixiaof:
博主真有毅力啊。
减肥日志
you really don't need much knowledge to use this script, it's pretty much plug and play, but we'll explain it's main part and how to use them anyway. If you have specific questions, please post them in our Javascript Forum and we will answer them there.
Set Cookie Javascript Function
You need to put the name and values in quotes when you call the function, like this:
Set_Cookie( 'mycookie', 'visited 9 times', 30, '/', '', '' );. Don't forget to put in empty quotes for the unused parameters or you'll get an error when you run the code. This makes the cookie named 'mycookie', with the value of 'visited 9 times', and with a life of 30 days, and the cookie is set to your root folder.
The Set_Cookie values for 'domain' and 'secure' are not utilized. Use 'domain' on the Javascript cookie if you are using it on a subdomain, like widgets.yoursite.com, where the cookie is set on the widgets subdomain, but you need it to be accessible over the whole yoursite.com domain.
It's good practice to not assume the path to the site root will be set the way you want it by default, so do this manually as a rule, '/'. If no value is set for expires, it will only last as long as the current session of the visitor, and will be automatically deleted when they close their browser.
function Set_Cookie( name, value, expires, path, domain, secure ) { // set time, it's in milliseconds var today = new Date(); today.setTime( today.getTime() ); /* if the expires variable is set, make the correct expires time, the current script below will set it for x number of days, to make it for hours, delete * 24, for minutes, delete * 60 * 24 */ if ( expires ) { expires = expires * 1000 * 60 * 60 * 24; } var expires_date = new Date( today.getTime() + (expires) ); document.cookie = name + "=" +escape( value ) + ( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + ( ( path ) ? ";path=" + path : "" ) + ( ( domain ) ? ";domain=" + domain : "" ) + ( ( secure ) ? ";secure" : "" ); }
This will set the cookie. It's the most complicated part, but actually for most purposes all you need to set are the first 4 parameters, name, value, expires, and path. If you want the cookie available only in one folder, you will need to add '/folder_name/' as a path variable, that tells the script to set the cookie for the just that path, not for the whole domain.
As a good general rule, set the path to '/', the root of your website. Generally 'domain' and 'secure' are not something you will be needing to use unless you set the cookie on a subdomain, in which case you may want to set the domain to your primary domain if you want it to be accessible from the main domain, or other subdomains.
Get Cookie Javascript Function
// this fixes an issue with the old method, ambiguous values // with this test document.cookie.indexOf( name + "=" ); function Get_Cookie( check_name ) { // first we'll split this cookie up into name/value pairs // note: document.cookie only returns name=value, not the other components var a_all_cookies = document.cookie.split( ';' ); var a_temp_cookie = ''; var cookie_name = ''; var cookie_value = ''; var b_cookie_found = false; // set boolean t/f default f for ( i = 0; i < a_all_cookies.length; i++ ) { // now we'll split apart each name=value pair a_temp_cookie = a_all_cookies[i].split( '=' ); // and trim left/right whitespace while we're at it cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, ''); // if the extracted name matches passed check_name if ( cookie_name == check_name ) { b_cookie_found = true; // we need to handle case where cookie has no value but exists (no = sign, that is): if ( a_temp_cookie.length > 1 ) { cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') ); } // note that in cases where cookie is initialized but no value, null is returned return cookie_value; break; } a_temp_cookie = null; cookie_name = ''; } if ( !b_cookie_found ) { return null; } }
Please note that the above version fixes some fairly serious issues with the old version, here listed below for comparison. The original utterly fails to handle cases where cookie names are substrings of other cookies, say like: site_main, site_main2 and so on. Thanks to ThoughtfulCoder for point this problem out. What's amazing to contemplate that this original code has been up for years without anyone noticing that weakness.
// this function gets the cookie, if it exists // don't use this, it's weak and does not handle some cases // correctly, this is just to maintain legacy information function Get_Cookie( name ) { var start = document.cookie.indexOf( name + "=" ); var len = start + name.length + 1; if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) { return null; } if ( start == -1 ) return null; var end = document.cookie.indexOf( ";", len ); if ( end == -1 ) end = document.cookie.length; return unescape( document.cookie.substring( len, end ) ); }
This will retrieve the cookie by name, if the cookie does not exist, it will return false, so you can do things like if ( Get_Cookie( 'your_cookie' ) ) do something.
Delete Cookie Javascript Function
// this deletes the cookie when called function Delete_Cookie( name, path, domain ) { if ( Get_Cookie( name ) ) document.cookie = name + "=" + ( ( path ) ? ";path=" + path : "") + ( ( domain ) ? ";domain=" + domain : "" ) + ";expires=Thu, 01-Jan-1970 00:00:01 GMT"; }
Here all you need to do is put in: Delete_Cookie('cookie name', '/', '') and the cookie will be deleted. Remember to match the cookie name, path, and domain to what you have it in Set_Cookie exactly, or you may get some very hard to diagnose errors.
WARNING: Do not assume the Cookie has actually been deleted during that session, both Opera and IE have in some of their browser versions maintained the cookie until you restart your browser even if you have deleted it in the script. If script functionality depends on the delete action having happened, make sure you do another Get_Cookie test on the deleted cookie to make sure it's actually deleted.
Sample Page Code for Javascript Cookies
<script type="text/javascript"> // remember, these are the possible parameters for Set_Cookie: // name, value, expires, path, domain, secure Set_Cookie( 'test', 'it works', '', '/', '', '' ); if ( Get_Cookie( 'test' ) ) alert( Get_Cookie('test')); // and these are the parameters for Delete_Cookie: // name, path, domain // make sure you use the same parameters in Set and Delete Cookie. Delete_Cookie('test', '/', ''); ( Get_Cookie( 'test' ) ) ? alert( Get_Cookie('test')) : alert( 'it is gone'); </script>
If you run this, what will happen is that you will get two alerts, the first one will have the content you set with Set_Cookie, the second will say 'it is gone' since you have now deleted the cookie. That's about it.
发表评论
-
php数据库备份和恢复
2011-07-22 13:41 8161)在php.ini里把disable_functions = ... -
判断4个字符串比较是否有重复的快捷算法
2011-05-31 13:53 952/** * 检查4个墙面名称,不能相同 */ fun ... -
Extjs 的 TextField 后面加上标记
2011-05-12 09:32 1682在输入框后面加上 * 作为标记,代码如下: var refi ... -
extjs遍历树节点
2011-03-30 16:17 3022//遍历所有的节点 ,除了编辑节点外,所有的节点采集标志 ... -
extjs--combobox下拉列表的triggerAction
2011-03-03 10:52 3033一般combobox的store先load加载数据,然后com ... -
在网页上屏蔽掉flash的超链接
2011-02-22 11:32 948var so = new SWFObject("./ ... -
extjs工作笔记1---属性表格去掉排序功能++很好的extjs例子网站
2011-02-11 09:46 874delete this.propertygrid.getS ... -
大图片预加载技术
2010-09-21 13:47 706图片预加载技术 var i = new Image(); i. ... -
javascript十六进制转换为补码
2010-09-19 14:52 2159转换为补码 var data = "FFFFFF5E ... -
javascript模块模式与全局变量
2010-09-16 10:03 598http://www.xiaozizai.com/blog/6 ... -
网站性能提高
2010-08-26 22:41 5921减少http请求 https://developer.moz ... -
div scrollTop置底以及FF与IE键盘事件
2009-07-16 18:50 19681)div 的css overflow:scroll, 并且高 ... -
高级js概念
2009-05-19 23:57 1982习惯于OOP语言编程后, ... -
动态加载js
2009-05-19 21:40 751是需要动态创建 <script> 才可以的. ... -
JavaScript不能做到的6,7事
2009-03-14 18:08 707JavaScript不能做到的6,7事 ... -
javascript管理cookie例子
2009-03-12 12:05 1302<script> //写cookies函数 作者 ...
相关推荐
js中cookie的创建和读取单个cookie值,以及设置cookie的过期时间。 能够判断是否有自己需要查找的cookie,如果没有查到到就可以执行创建语句
javascript对cookie的日常操作
主要介绍了javascript创建cookie、读取cookie的操作方法,内容简单易学,感兴趣的小伙伴们可以参考一下
这是一个asp.net与javascript混合操作cookie的例子,如添加、删除、修改、读取cookie
本文实例讲述了javascript cookie用法。分享给大家供大家参考,具体如下: 一、什么是 cookie? cookie 就是页面用来保存信息,比如自动登录、记住用户名等等。 二、cookie 的特点 同个网站中所有的页面共享一套 ...
Cookie是客户端存放数据的一种方式,可用来做状态保持。 1.设置Cookie: a.无过期时间:(若不设置过期时间,默认为会话级Cookie,浏览器关闭就会失效) 代码如下: function setCookie(name,value) { [removed] =...
cookie在开发中使用的非常多,但如果是使用JavaScript设置cookie然后使用PHP读取出来如何实现呢?即PHP与JavaScript下Cookie的交互使用是否可行呢? <?php // 读取JavaScript设置的cookie header(Content-type: ...
cookie javascript 应用 详解: 添加该cookie" onclick="add()" /> 读取所有cookie" onclick="getall()" /> 读取该名称cookie的值" onclick="get()" /> 删除该名称cookie" onclick="del()" /> </div>
java后台和php后台如何设置HttpOnly到前台浏览器的cookie中.cookie中设置了HttpOnly属性,那么通过js脚本将无法读取到cookie信息,这样能有效的防止XSS攻击.zip
用户每次访问站点时,Web 应用程序都可以读取 Cookie 包含的信息。 Cookie的出现是为了解决保存用户信息的问题。例如 当用户访问网页时,用户的名字可以存储在cookie中。 下次用户访问页面时,cookie会记住用户名。 ...
//创建文本字段: 名称为showbox,深度1,坐标位置10,10,宽200,高100 this.createTextField("showbox",1,10,10,200,100);...//这个对象用来接收从javascript传过来的数据,监视属性coo,当其数值改变的时候,自动触发函数show
在没介绍正文之前,先给大家介绍Cookie的基础知识 首先了解什么是cookie “cookie 是存储于访问者的计算机中的变量。每当同一台计算机通过... 在JavaScript中可以通过 [removed] 来读取或设置这些信息。由于 co
//此 cookie 将被保存 30 天 var exp = new Date(); //new Date("December 31, 9998"); exp.setTime(exp.getTime() + Days*24*60*60*1000); [removed] = name + "="+ escape(value) +";expires="+ exp....
JavaScript是运行在客户端的脚本,因此一般是不能够设置Session的,因为Session是运行在服务器端的。...解决这个问题的最好的方案是采用cookie来保存该变量的值,那么如何来设置和读取cookie呢? 首先需要稍微了
两个JS方法,分别是用JS对COOKIE的存入和读取的。
Cookie的读取和写入实现函数。
cookie本身是一些短小的信息,能够由页面保存在用户的计算机上,然后被其他页面读取。cookie一般都设置为在一定时间后失效。 当然,cookie也有局限之处:浏览器对于能够保存的cookie数量有所限制,通常是几百个或者...