开发者

function onload not working for IE7 and 8

开发者 https://www.devze.com 2022-12-09 11:59 出处:网络
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\">

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 Mozill开发者_JAVA百科a.

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


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);
     } 
} 


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"); });
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号