View Full Version : IE and prototype problem

12-13-2007, 09:41 PM
I need help to workaround the following problem:

First, about the environment:
- The web pages automatically generated.
- Pages can be inserted to each other
- When a subpage to be inserted is being generated, it does not know will it be inserted or not.
- That because each subpage uses the <script src=...> tags to load the script it needs.
- in the external script files I'm trying to protect it from repeated execution checking (if(){}) the value of a variable which is created after in the script.

Everything worked fine until I tried to use the .prototype to declare the method of my objects.
In the Internet Explorer, I got "Object does not support this property or method", on the access to a prototype function, because the prototype of my object was deleted by itself after the second load of the external script.

Here is the code sample:
<!-- content of the outer page -->
<script language="javascript" src="test.js"></script>
var a = new A;
<!-- content of the inserted subpage -->
<script language="javascript" src="test.js"></script>
var b = new A;

and the content of test.js is:

if( typeof js_loaded == "undefined" ) {
function A() { alert("constructor of A"); }
A.prototype.CustomFunc=function( p ) { alert("custom func."+p); };
var js_loaded = true;

the zip in attachment contains both of these two files
Please, help!

12-13-2007, 11:18 PM
Took me a while to figure it out. I had forgotten about an IE bug with conditional functions. IE will parse

function A() {...}

Even if it is inside a conditional. ECMAScript standards say otherwise.

You need to use a function statement instead:

var A = function() {...}

Also, keep your html tags lowercase for forward compatability. Also, the correct JS script tag should look like:

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


Working code:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

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

<script type="text/javascript">
var a = new A();

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

<script type="text/javascript">
var b = new A();


if(js_loaded === undefined) {
var A = function() {
alert("constructor of A");
A.prototype.customFunc = function(p) {
alert("custom func." + p);
var js_loaded = true;

Not that you need to, but using the whole if(js_loaded===undefined) is somewhat unweildy with multiple scripts. Perhaps design a loader script that logs the scripts it dynamically writes to the page and if it has been written, it simply skips it. In such a case, you wouldn't have to worry about using conditional functions and write normal function declarations.

12-13-2007, 11:40 PM
Thank you very much!!! It works!

12-14-2007, 09:52 PM
Took me a while to figure it out. I had forgotten about an IE bug with conditional functions.Actually, that's not a bug. The correct behaviour for a function declaration in a statement context is a syntax error, but implementations are allowed to extend the syntax, so having it do something instead of raising an exception is not a bug. However, SpiderMonkey, JavaScriptCore generally speaking do run time initialisation while JScript, Linear B and Futhark do compile time initialisation.

For more on this topic, see my posts to the es4-discuss list about this:
- <uri:https://mail.mozilla.org/pipermail/es4-discuss/2007-March/000483.html>
- <uri:https://mail.mozilla.org/pipermail/es4-discuss/2007-March/000495.html>

Which ended up in this very useful document on the differences between ES3 and JScript (with data on other ECMAScript engines as well.)

12-15-2007, 01:56 AM
Thanks for the post. Going to have to read that pdf; looks interesting.

I thought contrary to what you said because of the last paragraph in this subsection:

Regardless, better not to assume behavior and do it "right" the first time :D

Hopefully ECMAScript will set standards for the subject at hand.