NewBie
NewBie

Reputation: 21

function onload not working for IE7 and 8

My onload function is not working for IE7 and 8:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script type="text/javascript">

  function onload()
  {
    alert("Working properly")
  }

</script>
</head>
<body>

</body>
</html>

The alert doesn't happen if I try to access it with IE7 or 8 but it's working properly in Mozilla.

Can anyone suggest something which works for both IE and Mozilla?

Upvotes: 0

Views: 1818

Answers (2)

RameshVel
RameshVel

Reputation: 65877

This works only for IE.

Instead

function onload()  {    
       alert("Working properly") 
 }

try this..

function window.onload()  {    
       alert("Working properly") 
 }

EDIT:

Common approach for both browsers

function onload()  {    
  alert("Working properly") 
}    
var browserName=navigator.appName;     
if(browserName=="Microsoft Internet Explorer") 
{     
      window.onload=onload;   
} 
else 
{     
     if (document.addEventListener) 
     { 
         document.addEventListener("DOMContentLoaded", onload, false);
     } 
} 

Upvotes: 3

Palantir
Palantir

Reputation: 24182

Use a toolkit like JQuery or Mootools, which will take care entirely of these details for you.

In JQuery, you add the link to the jquery.js, then this would look like:

$(function() { alert("Working properly"); });

Upvotes: 0

Related Questions