开发者

how to use cookies in javascript and html?

my question is I took one form like register form and fill al fields like first name, last name,address, check boxes also used then when submit 开发者_JS百科at final i want it to display those data in new page. Here I want code only in javascript and html. Is their possible please help me........... and send code and guide lines


Yes, you could do that with cookies. Along those lines, I suggest using JQuery for your javascript development. JQuery makes cross-browser setting and retrieving cookies very easy.


Yes it's possible through plain javascript also. Try it like this:

document.cookie = "name=value; expires=date; path=path; domain=domain; secure";

Example:

var cookie_date_str = new Date ( 2011, 01, 19 );
document.cookie = "username=chand;
                  expires=" + cookie_date_str.toGMTString();


I suggest you don't use cookies any more, you can take advantage of the new HTML5 specifications and you can use the so called "localStorage" feature.
I recommend that you use jquery and it's magnificent plugins. A good example in this case is http://plugins.jquery.com/project/html5Storage , in wich case you could just do something like :

$('#formId')
  .children('input')
  .each(function(){
     $.Storage.set(
        $(this).attr('name'),
        $(this).val()
     );
  });

then,if you want to load the saved values , you will do something like :

$('#formId')
  .children('input')
  .each(function(){
     $(this).val($.Storage.get($(this).attr('name')));
  });

If tou want full browser compatibility , you should probably go with the cookie implementation, but localStorage is supported by webkit based browsers, firefox, opera and IE 8 :), so it's only a matter of old browsers compatibility.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜