Forum Moderators: open
I've included a function.js file in the [head] of my index.php file with this in it:
function hideDiv() {
documentGetElementById("cms").style="overflow:visible; display:none;";
}
Then I have this for my body tag so that it is meant to hide the div with id 'cms' when the page loads:
<body onload="hideDiv()">
But it doesn't concatenate the style to the div with id 'cms'!
Can anyone see the problem? Ideally I want to hide a few ids/divs on the same page when it loads so is there also a better way of doing this?
Thanks in advance
Is there actually a way of inserting a bit of javascript, say, just above the div I want to hide with something like the following code rather than having to specify each div id in the function itself?
<script type="javascript">hideDiv('cms');</script>
<div id="cms">
Content
</div>
and then later on in the page
<script type="javascript">hideDiv('other');</script>
<div id="other">
Content
</div>
I assume I'd need to place something like this in the function and place it in the head of the page right?
function hideDiv(id) {
document.getElementById(id).style.display = "none";
document.getElementById(id).style.overflow = "visible";
}
Cheers
A quick few questions about what you said:
1. I read on Tizag.com the following:
The location choice of head or body is very simple. If you want to have a script run on some event, such as when a user clicks somewhere, then you will place that script in the head. If you want the script to run when the page loads, like our "Hello World!" example in the previous lesson, then you will want to place the script within the body tag.
I've included my js in the head as an include - are you saying I should include it at the very end (before the end body tag)?
2. How would I load hideDiv when the DOM is ready? I'm using the scriptaculous library for a few basic effects (which has already massively slowed down my site btw - is that normal?), so can I use that library for this purpose?
Thanks once again
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" >
<!-- Styles -->
<link rel="stylesheet" type="text/css" href="yourfile.css">
<title>Untitled</title>
</head>
<body>
...
<!-- Scripts -->
<script type="text/javascript" src="yourscript.js"></script>
</body>
</html>
Event.observe(window, 'load', function() {
hideDiv('cms');
hideDiv('foo');
});