LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 07-27-2011, 02:21 PM   #1
lmcilwain
Member
 
Registered: Dec 2003
Location: Maryland
Distribution: Fedora 14
Posts: 367

Rep: Reputation: 30
jquery to dynamically create cookies using the jquery cookie plugin


hey guys,

I am learning jquery and I think for the most part I have the basics down. One of the things that I am attempting to do is to create cookies using the jquery cookie plugin. The idea is that I want to replace as much of the javascript in my code as possible with jquery to see the difficulties in doing it one way or the other.

As easy as it seems to be able to create a cookie using the plugin I have not been able to do some successfully and the only thing that I can think of is the way that I am submitting my parameters is not correct. So with that, here is is my code:

Code:
//loop over all input file of type text with class prompt
$('input:text, .prompt').each(function(){
//if the field has a value create a cookie then display the cookie in an alert
  if($(this).val() != ''){
    $name = $(this).attr('id');
    $value = $(this).val();
    $.cookie($name, $value;
    $alert($.cookie($name));
  };
});
Writing it out using javascript works perfectly fine.

Code:
$('input:text, .prompt').each(function(){
  if($(this).val() != ''){
    createCookie($(this).attr('id'),$(this).val());
  };
});

function createCookie(cookieName,cookieValue){
  var expDate = new Date(); // create a new date object
  expDate.setMonth(expDate.getMonth()+1); //set expiration date to 1 month from today
  document.cookie = cookieName+"="+escape(cookieValue)+";expires="+expDate.toGMTString(); //create cookie
}
Any idea of what I could be doing wrong that which makes it so that I am not able to create a cookie using the cookie plugin for jquery?
 
Old 07-27-2011, 08:25 PM   #2
phptek
LQ Newbie
 
Registered: May 2009
Location: Kapiti, NZ
Distribution: Fedora 14
Posts: 9

Rep: Reputation: 1
lmcilwain

I've not used the cookie functionality of jQuery before but you do have a typo in the $.cookie line:

Code:
$.cookie($name, $value;
..or was that a copy/paste issue?
 
Old 07-27-2011, 08:33 PM   #3
phptek
LQ Newbie
 
Registered: May 2009
Location: Kapiti, NZ
Distribution: Fedora 14
Posts: 9

Rep: Reputation: 1
lmcilwain;

I assume also you have the Cookie plugin? Not sure jQuery does cookies out of the box:

http://plugins.jquery.com/project/Cookie

Also, what is it that makes you think it doesn't work? Don't rely on that alert statement working right off the bat. You will often fall into a 'race condition' (actually not a real race condition, but close enough) where the time between setting a variable and getting that variable is too quick to expect it to work.

Try setting the cookie in jQuery in one function, and then is a separate function try and get it.

I've also never seen the '$' syntax used on your own variables.

Try this (not tested)

Code:
$('input:text, .prompt').each(function(){
//if the field has a value create a cookie then display the cookie in an alert
  if($(this).val() != ''){
    var name = $(this).attr('id');
    var value = $(this).val();
    $.cookie(name, value);
    alert($.cookie(name));
  };
});
Cheers
Russ
 
Old 07-27-2011, 09:02 PM   #4
lmcilwain
Member
 
Registered: Dec 2003
Location: Maryland
Distribution: Fedora 14
Posts: 367

Original Poster
Rep: Reputation: 30
Yes that was a copy paste issue sorry about that. Yes I do have the cookie plugin installed. Thought it would be nice to know if I installed it right. I simply stuck in the same place where my jquery.js file is and then included it in my html header tags.

Yes I did use the "$" for my variables because that is how I thought jquery used variables. Is that not the case?

I also wanted to have a create cookie method but could not figure out how to create another function using jquery code. In javascript I can simply call function someFunction() but in jquery I simply can't figure out how to do that same thing and then call it from within another function.

I will give your code a shot in a few minutes and let you know the results, thanks for the quick response.
 
Old 07-27-2011, 09:06 PM   #5
lmcilwain
Member
 
Registered: Dec 2003
Location: Maryland
Distribution: Fedora 14
Posts: 367

Original Poster
Rep: Reputation: 30
I just gave it a shot but it didn't seem to work. I don't see it throwing any errors in the firebug console as well. I will take another look at the documentation again, maybe I am not loading the cookie.js file correctly or something.
 
Old 07-27-2011, 09:11 PM   #6
phptek
LQ Newbie
 
Registered: May 2009
Location: Kapiti, NZ
Distribution: Fedora 14
Posts: 9

Rep: Reputation: 1
Quote:
Originally Posted by lmcilwain View Post
Yes I did use the "$" for my variables because that is how I thought jquery used variables. Is that not the case?
Short answer: I don't know, I've never tried. I've only ever used the '$' notation to grab whole DOM elements as-in $('#blah') instead of document.getElementById('blah');

Quote:
I also wanted to have a create cookie method but could not figure out how to create another function using jquery code. In javascript I can simply call function someFunction() but in jquery I simply can't figure out how to do that same thing and then call it from within another function.
Assuming you've included the files in this order:

1). jquery.min.js (or whatever it's called)
2). cookie.min.js (or whatever it's called)
3). my-custom-js-file.js (or whatever the name of your file is you're working on)

..then you can create function just as you've always done - the difference is now you have the power of jQuery to mix and match as you please.

Code:
function getMyID(someID) {
return document.getElementById(someID);
}
...is the same as

Code:
function getMyID(someID) {
return $(someID);
}
See also: https://github.com/carhartl/jquery-cookie for cookie examples

Good luck
Russ
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
JQuery/CSS Question filterfann Programming 0 01-05-2011 03:55 PM
LXer: jQuery: Novice to Ninja LXer Syndicated Linux News 0 04-29-2010 07:50 AM
LXer: New jQuery Forum is Here! LXer Syndicated Linux News 0 01-20-2010 07:40 PM
What is Json and jQuery and how are they related to Javascript? Romanus81 Programming 1 08-13-2009 09:35 PM
LXer: jQuery UI—The Dialog: Part 2 LXer Syndicated Linux News 0 02-11-2009 03:40 PM


All times are GMT -5. The time now is 12:28 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration