views:

6627

answers:

8

Hi all,

I am trying to make my pages work correctly with IE 8, I found out from here: http://www.masykur.web.id/post/How-to-Make-Our-Website-to-be-Ready-for-IE8.aspx that, my page has to be XHTML 1.0 compliant and atleast CSS 2.1 compliant, I made my page and CSS compliant with only few warnings, but still window.onload() is not firing. Does anybody encountered this problem?

here is the code snippet:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
    <head>
     <meta http-equiv="X-UA-Compatible" content="IE=8"/>
        <title>Testing</title>
        <link rel="stylesheet" href="test.css" type="text/css"></link>
        <script type="text/javascript" src="login.js"></script>
        <script type="text/javascript" src="common.js"></script>
        <script type="text/javascript">
      window.onload = function()
      {
                          // Not coming here at all on first shot   
      }
        </script>
    </head>
    <body>
     .
     .
     .

However refreshing the page seems to make it work.

Am I missing something here?

UPDATE:

One of the IE addons created this problem, after disabling its working fine. Thanks for your time and answers :)

A: 

onload fires after ALL your content has loaded (including external images etc). It's possible those resources are taking a long time to load on the first go (before they are cached). Another possibility is an error in your code that only affects IE as is stopping your scripts (but only the first time is odd).

SpliFF
First possibility is ruled out since, onload I am just calling a javascript function which sets a number. I am looking into any errors in my code, I just wrote a sample code which is working fine in IE 8 with onload (however onload content is blocked with this error: 'To help protect your security, Internet Explorer has restricted this website from running scripts...' everytime, I have to "Allow" to make it work).
Manohar
I found out that if Apache is rendering the page, onload is not firing. I have a sample code that can demonstrate this.
Manohar
I think you might misunderstand. It doesn't matter what your onload handler actually does, what matters is it won't even run _until_ every page element has "loaded" (not just the html, but external CSS, JS and images). If IE is getting stuck downloading an external resource or running inline JS then it hasn't "loaded" and your script won't fire. You may have a loop somewhere in your code that is preventing "loading" from completing.
SpliFF
but more likely some interaction with Apache is blocking a resource from downloading. Do you have any modrewrite rules that may be affecting content?
SpliFF
PS. In particular look for anything that may be causing redirects or recusion.
SpliFF
Actually there was a IE plugin which was causing this problem. I never knew plugins could cause this sort of problems until now :).
Manohar
A: 

If you are getting different results using Apache vs. another web server (IIS?) and comparing the end result using IE8, then the differrence must be in the content type header being sent. Get the wget utility for your platform and see the headers that are produced. If you are on Windows, then the Portable Apps version of the GUI wget is pretty nice.

Tony Miller
I have not tried in IIS, I have tried in apache only and its not working for me, I tried using window.onload = new Function("alert('!');"); also, even this is not firing in IE-8.
Manohar
A: 

The following code works for me. When I load the page in Firefox I see the alert instantly. When I first load the page in IE 8 it warns me about active content. If I allow the blocked content it asks me to confirm, which I do. Then the alert appears as expected. If this does not work for you, try IE 8 on a different computer or start eliminating code in your page to check for errors. You could do a binary search: comment out the first half of the page and see if the alert appears; if it still does not, then uncomment out the first half and comment out the second half. Repeat as needed until you've narrowed it down to the offending code. Incidentally you don't need XHTML for IE8 compliance. HTML works fine and actually has some advantages.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=8"/>
    <title></title>
    <style type="text/css">
    </style>
    <script type="text/javascript">
    window.onload=function() { alert('hello');};
    </script>
  </head>
  <body>
  </body>
</html>
Mr. Shiny and New
It does work as you describe if I derectly open the HTML file with IE 8. But when I put the same file in apache and access it through the server, onload is not firing for the first time. I have to manually refresh everytime to make the code block in onload to work. I have not yet tried in different machines. I will try and update here.
Manohar
I suspect it's related to xhtml then. Try using regular HTML.
Mr. Shiny and New
I should add: if it is an XHTML problem, the solution might be Fran Cooper's solution, which is to put CDATA wrappers around the javascript.
Mr. Shiny and New
+2  A: 

You could have an error in your JavaScript's, if that happens, any JavaScript after that will not function correctly.

Try to remove the reference to login.js and common.js and try an alert within your problematic function.

Ólafur Waage
A: 

I don't have IE8 to personally test, but what does this test do?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Test IE 8</title>
<script type="text/javascript">
/* <![CDATA[ */
window.onload = function(){alert('Good morning!');}
/* ]]> */
</script>
</head>
<body>
<h1>Hello</h1>
<body>
</html>

If this test works as expected, try the CDATA bit around your internal JavaScript block.

And then, if that does not work as expected, there is probably something in the external JavaScript above it that prevents your onload from firing. The previous poster mentioned this. At that point, try your error console or debugger to point the way.

Fran Corpier
A: 

Manohar, Which IE Addon caused the problem? I am facing the same issue. thanks, Pete

Pete Heller
Yes. It was a rogue plugin developed for IE caused the problem. Try disabling All the plugins and then run your JS.
Manohar
+1  A: 

For IE try:

window.onload = new function() { alert('hello');};
George
A: 

Are there some flash in yout page? The flash addon causes the problema. With some old versions of flash player the problem doesnt occur.

Calixto