tags:

views:

53

answers:

5
<script language="javascript">
function toggle(id) {
    alert('call');
    if (document.getElementById(id).style.display == "none") {
        alert('now visible');
        document.getElementById(id).style.display = "";
    } else {
        alert('now invisible');
        document.getElementById(id).style.display = "none";
    }
}
</script>

</head>
<body onload="toggle('image1');alert('test_body');toggle('image2')">

<script language="javascript">
alert('test_pre_function');
toggle('image1');
alert('test_after_function');
toggle('image2');
</script>

Looks like a lot of code but it's pretty simple so i think most of you won't have troubles with it. toggle() should toggle the display status of divs containing images.

When the user enters the site the divs should hide, when everything is loaded the divs should show up. (onload)

Strangely enough, the funtion in the body (not in the body tag) only work half, i get and alert 'test_pre_function' and i get an alert 'call' (out of the function), but that's it. The code in the body tag runs just fine.

I find this weird because it's supposed to do exactly the same twice and one time it runs, another time not, so i guess i must have made some stupid mistake.

Thanks for any help!

+3  A: 

Script is executed as soon as it is parsed. If image1 and image2 haven't been parsed when the script is executed, document.getElementById("image1") will return null so .style.display will throw a "is null or not an object error". This explains why the two alerts work - execution stops at the first document.getElementById(id).style.display == "none" line.

Move the script to after the image elements in the document and it should work.

<script>
    alert(document.getElementById("image1")); // -> null
</script>
<img id="image1" src="some/image.jpg" />
<script>
    alert(document.getElementById("image1")); // -> object
</script>
Andy E
of course *dow* thanks!
Samuel
A: 

Are you missing this?

alert('now visible');
document.getElementById(id).style.display = "block";
Pandiya Chendur
No that works without :)
Samuel
@Samuel Andy's answer is worth a try..
Pandiya Chendur
@Pandiya: i know, it is the solution, i cant mark it answered yet though, have to wait 2 more min
Samuel
A: 

Try this http://jsfiddle.net/hUDb4/

var toggle = (function() {
    // an object to keep state for elements
    var state = {};
    return function(id){
        document.getElementById(id).style.display = (state[id] = !state[id]) ? "none" : "block";
    };
})();


toggle("myid"); // none
toggle("myid"); // block

But the main issue here is as others has stated that the DOM isn't ready until the document is fully loaded.

Sean Kinsey
A: 

Hi, you have used alert in the body onload. basically once the alert will get executed, then it asks you to click OK. meanwhile the last function may gets delayed focus.

see below

<script>
    alert(document.getElementById("image1"));
    <img id="image1" src="some/image.jpg" />
    alert(document.getElementById("image1"));
</script>
VAC-Prabhu
That's some fantastic copy-pasting.
Andy E
A: 

Ummm, ideally you should hide the images via CSS (<div id="image1" style="display: none;"></div>); then unhide them once the document is loaded. People saying that the DOM is not available when you call the function first time are right. Or may be its just the two DIVs that are not available because you declare them in the source after calling the toggle function.

Salman A