Tuesday, June 26, 2007

Catch the Error and remember the tip!!

Problem: I am trying to write a small test program but it doesn't seem to run in IE though it runs absolutely fine in FF!!


Here is my HTML

 <script type="text/javascript" src="somescript.js" />

 <div style="border:1px solid black;height:10px;width:100px;" onclick="test();"></div>


Here is the somescript.js

function test(){




Try finding the catch here before reading for answer.



Solution: If this were a real page, the results would have been pretty much the same, no matter what or how much content was on it. A blank page! You might even get some JavaScript errors thrown in too. Errors like "object expected", for an object you know for sure exists.

The problem is that the <Script> element cannot be self closing. It's interpreted as having no closing tag, and so all of the content after the script element is inside the script element -- and therefore does not appear in the visible output. It also makes dom elements that you'd normally expect to be around non-existent, which can cause errors in any script that does happen to load. So you might get a JavaScript error, followed by the blank page.

Change your script tag like so, and problem solved:

<script type="text/javascript" src="somescript.js" ></script>

This appears to be an IE issue -- works fine in FF and may other browsers too!


Tip: Do not try to self-close all the tags. Some of them are supported while others are not. It’s always a good idea to have full closing tag wherever possible.




Thanks & Regards,

Arun Manglick

SMTS || Microsoft Technology Practice || Bridgestone - Tyre Link || Persistent Systems || 3023-6258


DISCLAIMER ========== This e-mail may contain privileged and confidential information which is the property of Persistent Systems Pvt. Ltd. It is intended only for the use of the individual or entity to which it is addressed. If you are not the intended recipient, you are not authorized to read, retain, copy, print, distribute or use this message. If you have received this communication in error, please notify the sender and delete all copies of this message. Persistent Systems Pvt. Ltd. does not accept any liability for virus infected mails.

1 comment: