Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    zc2
    zc2 is offline
    New to the CF scene
    Join Date
    Dec 2007
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    IE and prototype problem

    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:
    <HTML><BODY>
    <!-- content of the outer page -->
    <script language="javascript" src="test.js"></script>
    <script>
    var a = new A;
    a.CustomFunc(1);
    </script>
    <!-- content of the inserted subpage -->
    <script language="javascript" src="test.js"></script>
    <script>
    var b = new A;
    b.CustomFunc(2);
    </script>
    </BODY>
    </HTML>


    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!
    Attached Files Attached Files

  • #2
    Regular Coder
    Join Date
    Jun 2007
    Location
    USA
    Posts
    527
    Thanks
    26
    Thanked 74 Times in 72 Posts
    Took me a while to figure it out. I had forgotten about an IE bug with conditional functions. IE will parse
    Code:
    function A() {...}
    Even if it is inside a conditional. ECMAScript standards say otherwise.

    You need to use a function statement instead:
    Code:
    var A = function() {...}
    Also, keep your html tags lowercase for forward compatability. Also, the correct JS script tag should look like:
    Code:
    <script type="text/javascript" src="test.js"></script>

    ************************

    Working code:
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <title>Test</title>
    </head>
    <body><div>
    
    <script type="text/javascript" src="test.js"></script>
    
    <script type="text/javascript">
    var a = new A();
    a.customFunc(1);
    </script>
    
    <script type="text/javascript" src="test.js"></script>
    
    <script type="text/javascript">
    var b = new A();
    b.customFunc(2);
    </script>
    
    </div></body>
    </html>
    Code:
    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.

  • #3
    zc2
    zc2 is offline
    New to the CF scene
    Join Date
    Dec 2007
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you very much!!! It works!

  • #4
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Quote Originally Posted by Trinithis View Post
    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/e...ch/000483.html>
    - <uri:https://mail.mozilla.org/pipermail/e...ch/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.)
    <uri:http://wiki.ecmascript.org/lib/exe/f...onsfromes3.pdf>
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #5
    Regular Coder
    Join Date
    Jun 2007
    Location
    USA
    Posts
    527
    Thanks
    26
    Thanked 74 Times in 72 Posts
    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:
    http://developer.mozilla.org/en/docs...ing_a_function

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

    Hopefully ECMAScript will set standards for the subject at hand.
    Trinithis


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •