Forum Moderators: open
Can anyone help?
Thanks!
un
<script type="text/javascript">
function setDivCookie() {
document.cookie="hidediv";
}
function checkDivCookie() {
if (document.cookie.indexOf("hidediv")!=-1) {
document.getElementById("linkdiv").style.visibility="hidden";
}
}
</script>
...
<div id="linkdiv" style="visibility:visible">
<a href="..." onclick="setDivCookie()">Link 1</a>
<a href="..." onclick="setDivCookie()">Link 2</a>
...
</div>
...
<script type="text/javascript">
checkDivCookie();
</script> I tend to stay away from using the "onload" event to manipulate document objects, like a DIV element, as it has been unreliable. Often the onload event will fire before the page is truly loaded, and the object I want to manipulate hasn't been fully initialized, and an error results. Therefore I tend to place the function call at the end of the page code, near </body>, because once that enters the browser's memory, the previous code has definitely been initialized.
I tend to stay away from using the "onload" event to manipulate document objects, like a DIV element, as it has been unreliable. Often the onload event will fire before the page is truly loaded....
Really?! Certain browsers? I have always thought the onload event to be the prefered method (can't say I've had a problem in the past)...?
Therefore I tend to place the function call at the end of the page code...
Is this more 'reliable'?! But this doesn't go all the way to separating your JS behaviours from your content, and besides, to have to add extra function calls at the end of the markup is an increased overhead to say the least, particulary if you are adding functionality to many pages. Just curious... :)
Wish I could do that...
I did manage to add an expiry to it so it extends across sessions by myself, but that's the limit of my JS talents!
If you have a minute, could you give a small explanation of what it does? In particular the "if (document.cookie.indexOf("hidediv")!=-1)" bit...
function setDivCookie() {
// Add "hidedev" to the cookie's string
// Creates a new cookie if it doesn't already exist
document.cookie="hidediv";
}
function checkDivCookie() {
// Does the cookie string contain "hidedev"?
// Basically, does the statement NOT evaluate to "false"
// "true" can be any positive integer, so it's best to test for "not-false"
// "false" can be either 0 or -1
// If it's 0, then document.cookie.indexOf("hidediv") is
// not available for comparison, and so returns "false" (-1)
if (document.cookie.indexOf("hidediv")!=-1) {
document.getElementById("linkdiv").style.visibility="hidden";
}
} Really?! Certain browsers? I have always thought the onload event to be the prefered method (can't say I've had a problem in the past)...?
Is this more 'reliable'?!
But this doesn't go all the way to separating your JS behaviours from your content
to have to add extra function calls at the end of the markup is an increased overhead to say the least
...an increased overhead...
I actually meant in development time (as opposed to runtime), since you might link the JS file in the <head> section (possibly with a server-side script), but would have to add a call at the end of the body as well - just thinking if you were including the script in many files.
Thanks for the response - I see what you're saying, in fact I may have come across 'this problem' recently and not appreciated it. I noticed a text/image replacement script, called from the window.onload event very 'occasionally' didn't do its thing - a refresh would sort it and could never reproduce it, but must have a closer look!
Sorry, digressed a tad from the original Cookie query...