Web Hosting Talk







View Full Version : Trouble with my JavaScript Functions


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?

bimbiero
09-13-2007, 12:01 AM
Just a thought, did you confirm that the actual javascript file that you imported actually imported?


<script type="text/javascript" src="inc/javascript_functions.js"></script>



I have had an issue like this when the external javascript didn't load in, because it doesn't actually throw an error.

A simple test might to go to your site.. www[.]yoursite[.]com/inc/javascript_functions.js and see if that has you download / opens up for you.

lemme know if that helped.

latheesan
09-13-2007, 06:26 AM
Good point, i should have did this. I got very important config files in the /inc/ dir, so i made a .htaccess to block everything in there and give permission to only the server to access it.

It seems, it only works with php, not javascript. lol, silly error. I think i know how to get this working, thanks for that =)

latheesan
09-13-2007, 06:46 AM
I got the Js to be properly included now. When i press close, the module closes properly. So, i clicked on another page to see if page loads with that module closed, but instead i get this error:

Error: c has no properties

The error is coming from this code:

//
// Function To Close A Module
//
function Close(Module)
{
var m = Module;
c = document.getElementById(m);
c.style.visibility = 'hidden';
c.style.height = '0px';
}

nnormal
09-13-2007, 11:10 AM
its possible you are calling the function before the element you referenced had been rendered. You may need to have the bit that checks your modules fire after the page has loaded (foolproof way is to copy paste the block to the bottom of the page, the "right" way is to use event handlers).

HostSentry
09-13-2007, 11:54 AM
I got the Js to be properly included now. When i press close, the module closes properly. So, i clicked on another page to see if page loads with that module closed, but instead i get this error:



The error is coming from this code:

//
// Function To Close A Module
//
function Close(Module)
{
var m = Module;
c = document.getElementById(m);
c.style.visibility = 'hidden';
c.style.height = '0px';
}


Not sure what module is about...

Try:

//
// Function To Close A Module
//
function Close(Module)
{
document.getElementById(Module).style.visibility = 'hidden';
document.getElementById(Module).style.height = '0px';
}

latheesan
09-14-2007, 06:17 AM
Thanks for your suggestions, im afraid none of these worked =(