latheesan
09-12-2007, 10:26 PM
I wrote this function to basically open/close module blocks on my site and whilst im doing this, set a cookie, so when the site loads, a check is made everytime to see if the page shud be created with with certain module closed/opened.
The functions looks like this:
//
// Function To Create Cookie
//
function createCookie(name,value,days)
{
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}
//
// Function To Read Cookie
//
function readCookie(name)
{
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var 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;
}
//
// Function To Erase Cookie
//
function eraseCookie(name)
{
createCookie(name,"",-1);
}
//
// Function To Close A Module
//
function Open(Module)
{
var m = Module;
o = document.getElementById(m);
o.style.visibility = 'visible';
o.style.height = '';
}
//
// Function To Open A Module
//
function Close(Module)
{
var m = Module;
c = document.getElementById(m);
c.style.visibility = 'hidden';
c.style.height = '0px';
}
This is how i loaded the function into the site on the index.php between the <head></head> tags:
<script type="text/javascript" src="inc/javascript_functions.js"></script>
After loading the JS Functions, i have this short script to determine which module starts closed on the index.php
<script type="text/javascript">
var Mod_Name = readCookie("Mod_Name");
if(Mod_Name == "Close")
{
Close("Mod_Name");
}
</script>
This is how i use it:
<a href="javascript:Open('Mod_Name');" onclick="createCookie('Mod_Name','Open',7);">Open Module</a>
<a class="text_grey" href="javascript:Close('Mod_Name');" onclick="createCookie('Mod_Name','Close',7);">Close Module</a>
So, to test this, i clicked the close button for the first sample module. this is the error i got in firefox:
Error: readCookie is not defined
Error: createCookie is not defined
Error: Close is not defined
... Where im i going wrong? Can someone help me with this please?
The functions looks like this:
//
// Function To Create Cookie
//
function createCookie(name,value,days)
{
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}
//
// Function To Read Cookie
//
function readCookie(name)
{
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var 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;
}
//
// Function To Erase Cookie
//
function eraseCookie(name)
{
createCookie(name,"",-1);
}
//
// Function To Close A Module
//
function Open(Module)
{
var m = Module;
o = document.getElementById(m);
o.style.visibility = 'visible';
o.style.height = '';
}
//
// Function To Open A Module
//
function Close(Module)
{
var m = Module;
c = document.getElementById(m);
c.style.visibility = 'hidden';
c.style.height = '0px';
}
This is how i loaded the function into the site on the index.php between the <head></head> tags:
<script type="text/javascript" src="inc/javascript_functions.js"></script>
After loading the JS Functions, i have this short script to determine which module starts closed on the index.php
<script type="text/javascript">
var Mod_Name = readCookie("Mod_Name");
if(Mod_Name == "Close")
{
Close("Mod_Name");
}
</script>
This is how i use it:
<a href="javascript:Open('Mod_Name');" onclick="createCookie('Mod_Name','Open',7);">Open Module</a>
<a class="text_grey" href="javascript:Close('Mod_Name');" onclick="createCookie('Mod_Name','Close',7);">Close Module</a>
So, to test this, i clicked the close button for the first sample module. this is the error i got in firefox:
Error: readCookie is not defined
Error: createCookie is not defined
Error: Close is not defined
... Where im i going wrong? Can someone help me with this please?
