score:0

Accepted answer

In your case you do not need to wait due to the location of your script. The script wont be parsed/executed till the elements above it are parsed. Therefore, it's safe to assume the contentMain div and it's descendants are ready to be manipulated.

{%include file='/var/www/include/tpl/header.tpl'%}
<div id="contentMain">
        <h1>{%$heading%}</h1>
        {%if isset($Details)%}
                <h4>Details</h4>
                {%$Details%}
        {%/if%}
        {%$table%}
</div>
<div id="space">
&nbsp;
</div>
<script>
    alert("It worked!");
    //run some stuff here
</script>
{%include file='/var/www/include/tpl/footer.tpl'%}

score:0

You can do this using jquery's document.ready.

On the header, Include jquery reference.

<script type="text/javascript">
  $(document).ready(function() {
     alert("It worked!");
  });
</script>
</head>

score:3

jQuery has a handy-dandy little function that you can attach to the document to run functions once the body has loaded. It is the ready function.

$(document).ready(function() {
    //Place code here to do stuff
});

Doing it this way, you can ensure that the body and all of its content has loaded before any of the stuff inside of the function runs.


Related Query

More Query from same tag