|
|||||||
| JavaScript JavaScript (not Java) Discussion and technical support, including AJAX and frameworks (JQuery, MooTools, Prototype...) |
![]() |
|
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
JAvascript with cookies help!!!
Hello...
I have this problem with Cookies and Drop Down BG Select When ever I select a background image, it works... But if I exit it... The image I selected will not automatically appear... Why doesnt the cookies work? Things: I tried it with IE 7 or Mozila 2.03 I did not make this script This purpose is for offline use... Its javascript Here is the Script Quote:
Last edited by Vicboy; 05-18-2007 at 08:16 AM. |
|
#2
|
||||
|
||||
|
You need to write the cookie value back to document.cookie correctly. It doesn't appear you are. I created a small JavaScript library to handle cookies:
Code:
/**
* @class Cookies
* Easy management of document cookies. Also contains a function to test
* if cookies are enabled. The read, delete and write functions are taken
* from Quirksmode.org (http://www.quirksmode.org/js/cookies.html).
* Basically I just made it object oriented, and slightly rearranged it
* to suit my programming preferences. :)
*/
var Cookies = {
/**
* @class Cookies
*
* @function enabled
* Checks to see if the browser has cookies enabled.
*
* @param void
*
* @return boolean
* True if cookies are enabled, false otherwise.
*/
enabled: function() {
return navigator.cookieEnabled ? true : false;
},
/**
* @class Cookies
*
* @function delete
* Deletes the given cookie.
*
* @param name (string, required)
* The name of the cookie to delete.
*
* @return void
*/
delete: function(name) {
this.write(name, "", -1);
},
/**
* @class Cookies
*
* @function read
* Reads the cookie value and returns it.
*
* @param name (string, required)
* The name of the cookie to read.
*
* @return string
* The string value of the cookie.
*/
read: function(name) {
var nameEQ = name + "=", ca = document.cookie.split(';');
var c = null;
for(var i=0;i < ca.length;i++) {
c = ca[i];
while (c.charAt(0)==' ') {
c = c.substring(1,c.length);
}
if (c.indexOf(nameEQ) == 0) {
return c.substring(nameEQ.length,c.length);
}
}
return null;
},
/**
* @class Cookies
*
* @function write
* Saves a cookie to the document.cookie string.
*
* @param name (string, required)
* The name of the cookie to write.
*
* @param value (variable, required)
* The value to store in the cookie.
*
* @param days (number, optional)
* The number of days the cookie should be stored. If the number of days
* isn't given, the cookie is valid until the end of the browser
* session, when the browser is closed.
*
* @return void
*/
write: function(name, value, days) {
var expires = "";
if (days) {
var date = new Date();
expires = "; expires="+date.toGMTString();
date.setTime(date.getTime()+(days*24*60*60*1000));
} else {
expires = "";
}
document.cookie = name+"="+value+expires+"; path=/";
}
};
1. Read the mthbkg cookie and store its value (var mthbkg = Cookies.read('mthbkg');) 2. Apply that background image to the BODY element. 3. When the SELECT is updated, store the new background image value in the mthbkg cookie.
__________________
My Blog: FundaMental Disaster Accessible DHTML Tabs | Quick CSS Positioning Explanation | Quick Floated Elements Explanation | 50% + 50% != 100% | Gaps Under Images | ID vs. Class | Gappy Lists in Internet Explorer | Why Tables Are Slower | Benefits of XHTML vs. HTML | Linking to External Style Sheets | About DOCTYPES | Web Design is a Peanut Butter & Jelly Sandwhich | CSS: To Hack Or Not To Hack | Internet Explorer and Transparent PNGs |
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|