use*_*566 6 html javascript cookies jquery
I'm trying to save certain data into cookies, in such a way that if I jump from one page to another, the data entered should be saved and retrieved back on refresh. I have a link:
<div class="esc-policy">
<a href="#">New Link</a>
</div>
Run Code Online (Sandbox Code Playgroud)
before i fire the click event i need to save the entered the following data fields:
<table class="form">
<tr>
<td class="label">
<label>Name*</label>
<input id="nameInput" type="text" maxlength="100"/>
</td>
<td class="label">
<label>Description</label>
<input id="descriptionInput" type="text" maxlength="200" >
</td>
</tr>
</table>
Run Code Online (Sandbox Code Playgroud)
I have something like this as my js:
if ($.cookie('back_to_url_onPage_referesh')) {
this.name.attr("value", $.cookie('name'));
this.description.attr("value", $.cookie('description'));
}
Run Code Online (Sandbox Code Playgroud)
I'm not sure of how this works though. Can someone please help!!
Thanks in advance...
I am able to save the values in the cookie..however I still dont get how do i retrieve it while jumping back from one url to another. Ex:i have a url: ('#/link/create') where im creating this cookie..onclick of <a href="#">New Link</a>..i need to navigate to another url '#/new-link/create', enter few form fields on that page and when i hit the Save button- <a href="#" class="btn">Save</a>, it should take me back to this url: ('#/link/create')..when i navigate between pages, the data is gone ofcourse, how to save that data on the UI side???
$(".esc-policy a").on('click',function(){
var name = $("#nameInput").val(),
description = $("#descriptionInput").val();
$.cookie('back_to_url_onPage_referesh', 1);
$.cookie('name',name);
$.cookie('description',description);
});
Run Code Online (Sandbox Code Playgroud)
如果你添加这个代码,它将在你的js中工作.如果你不在jquery中使用cookie插件.使用native document.cookie表达式,或者使用cookie函数扩展jquery对象
你可以使用这个插件 https://code.google.com/p/cookies/wiki/Documentation#With_jQuery
要使用Cookies,请检查jQUery Cookie插件
https://github.com/carhartl/jquery-cookie
然后你可以这样做:
$.cookie("test", 1);
Run Code Online (Sandbox Code Playgroud)
删除:
$.removeCookie("test");
Run Code Online (Sandbox Code Playgroud)
此外,要在cookie上设置特定天数(此处为5)的超时:
$.cookie("test", 1, { expires : 5 });
Run Code Online (Sandbox Code Playgroud)
如果省略expires选项,则cookie将成为会话cookie,并在浏览器退出时被删除.
涵盖所有选项:
$.cookie("test", 1, {
expires : 10, //expires in 10 days
path : '/', //The value of the path attribute of the cookie
//(default: path of page that created the cookie).
domain : 'jquery.com', //The value of the domain attribute of the cookie
//(default: domain of page that created the cookie).
secure : true //If set to true the secure attribute of the cookie
//will be set and the cookie transmission will
//require a secure protocol (defaults to false).
});
Run Code Online (Sandbox Code Playgroud)
要回读cookie的值:
var cookieValue = $.cookie("test");
Run Code Online (Sandbox Code Playgroud)
如果cookie是在与当前cookie不同的路径上创建的,您可能希望指定path参数:
var cookieValue = $.cookie("test", { path: '/foo' });
Run Code Online (Sandbox Code Playgroud)