Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Setting cookie in JavaScript
New Posts  All Forums:Forum Nav:

Setting cookie in JavaScript

post #1 of 3
Thread Starter 
Why am I unable to create a cookie with the code below? I'm returning the exact data I need to create a cookie, but for some reason it won't work.

I've run alerts to check to make sure it's valid data (it is) and I still can't set the darn cookie.

Help!

Code:
function Cookie(){
this.cookieName;
this.cookieExpiration;
this.cookiePath;

this.setName = function(name, value){
this.cookieName = String(name + '=' + value + '; ');
return this; 
}

this.setExpirationDate = function(expiration, format){
var date = new Date();
date.setTime(date.getTime());
this.cookieExpiration = String('expires=' + date.toGMTString() + '; ');
return this;
}

this.setPath = function(path){
this.cookiePath = String('path=');
if(path == null || path == undefined || path == '')
{
this.cookiePath += String('/');
}
else {
this.cookiePath += String(path);
}

return this;
}

this.createCookie = function(){
alert(this.cookieName + this.cookieExpiration + this.cookiePath);
document.cookie = '' + this.cookieName + this.cookieExpiration + this.cookiePath + '';
alert(document.cookie);
}
}

var cookie = new Cookie();
cookie.setName('name1', 'val1').setExpirationDate().setPath().createCookie();
Primary PC
(13 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II 720 (2.8 ghz) BIOSTAR A780L3L (AM3) MSI GeForce 9800GT G.Skill Ripjaws Series DDR3 1600 (8GB) 
Hard DriveOSMonitorPower
Crucial M4 64GB / Samsung F3 1TB Windows 7 Pro (64-bit) Dell SP2309W (2048x1152) PC Power & Cooling 610W 
CaseMouse
Cooler Master Elite 430 RC Logitech MX518 
  hide details  
Reply
Primary PC
(13 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II 720 (2.8 ghz) BIOSTAR A780L3L (AM3) MSI GeForce 9800GT G.Skill Ripjaws Series DDR3 1600 (8GB) 
Hard DriveOSMonitorPower
Crucial M4 64GB / Samsung F3 1TB Windows 7 Pro (64-bit) Dell SP2309W (2048x1152) PC Power & Cooling 610W 
CaseMouse
Cooler Master Elite 430 RC Logitech MX518 
  hide details  
Reply
post #2 of 3
Edit: Your code is working fine - it sets the cookie for me.
Check to see what might be blocking it externally in your browser.
Edited by ALUCARDVPR - 8/11/11 at 8:10pm
post #3 of 3
well i have NO idea why you wouldn't build the cookie server side and send it in the resonse header... with http only and https flags set of course.
stable again
(25 items)
 
  
CPUCPUMotherboardGraphics
E5-2687W E5-2687W ASUS Z9PED8-WS EVGA GTX 570 (Linux host) 
GraphicsRAMHard DriveHard Drive
EVGA GTX 970 FTW (win7 guest) 64GB G.SKILL 2133 2x Crucial M4 256GB raid1 4x 3TB raid 10 
CoolingCoolingCoolingCooling
2x Apogee HD  2x RX 480 2x MCP 655 RP-452x2 rev2 (new) 
CoolingCoolingOSOS
16x Cougar Turbine CFT12SB4 (new) EK FC 580 Gentoo (host) Gentoo (x23 guests) 
OSMonitorMonitorPower
windows 7 (guest w/ vfio-pci) Viewsonic 23" 1080P Viewsonic 19" Antec HCP Platinum 1000 (new) 
CaseOtherOther
Case Labs TH10 (still the best ever) 2x Lamptron FC-5 IOGEAR 2 way DVI KVM Switch 
  hide details  
Reply
stable again
(25 items)
 
  
CPUCPUMotherboardGraphics
E5-2687W E5-2687W ASUS Z9PED8-WS EVGA GTX 570 (Linux host) 
GraphicsRAMHard DriveHard Drive
EVGA GTX 970 FTW (win7 guest) 64GB G.SKILL 2133 2x Crucial M4 256GB raid1 4x 3TB raid 10 
CoolingCoolingCoolingCooling
2x Apogee HD  2x RX 480 2x MCP 655 RP-452x2 rev2 (new) 
CoolingCoolingOSOS
16x Cougar Turbine CFT12SB4 (new) EK FC 580 Gentoo (host) Gentoo (x23 guests) 
OSMonitorMonitorPower
windows 7 (guest w/ vfio-pci) Viewsonic 23" 1080P Viewsonic 19" Antec HCP Platinum 1000 (new) 
CaseOtherOther
Case Labs TH10 (still the best ever) 2x Lamptron FC-5 IOGEAR 2 way DVI KVM Switch 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Setting cookie in JavaScript