Friday, January 25, 2008

How to load a Javascript on a page without using the body or img tag.

If you need to execute some Javascript function before loading the page but you can't use or don't want to use the tags <body> nor <img> for that do the following:

<script type="text/javascript">
function ShowFirst(){ //do something }
</script>

<script language="javascript" type="text/javascript" for="window" event="onload">
if (document.all) {
//For IE
ShowFirst();
}else{
//For Firefox
window.document.addEventListener("DOMContentLoaded", ShowFirst, true);
}
</script>

No comments: