tags:

views:

84

answers:

3

Hi All,

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"&gt;
<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?

A: 

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"); });
Palantir
i am not using jquery
Ankita
+1  A: 

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);
     } 
}
Ramesh Vel
your code works for IE but not for mozilla....
Ankita
Check my updated answer
Ramesh Vel
Thanks it is working if found only one thing need to change function name from onload to some thing else than it work fine.If we keep name as onload will not work for IE..Many thanks for this again
Ankita
fine.. mark it as answer if this helps u..
Ramesh Vel
A: 

Writing straight javascript is good in that you will get a sense of what the differences are between javascript engines on the different browsers but at a certain point you will want to choose a framework. As Palantir mentioned using a javascript framework will greatly increase your productivity. Writing straight javascript it too riddled with cross browser issues i.e. your function works in one browser and not another. A framework, think of it as a library, smooths those differences between browsers a bit while adding other features that make programming for the web easier and honestly more fun. I prefer mootools http://mootools.net/ but jquery is definitely the more popular of the two. There is a nice introductory tutorial on using mootools here.

moorej