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 6 of 6
  1. #1
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts

    Moz weird dynamically behaviour

    PHP Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  "http://www.w3.org/TR/html4/loose.dtd">
    <
    html>
    <
    head>
    <
    title>Untitled Document</title>
    <
    meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <
    meta http-equiv="Content-Style-Type" content="text/css">
    <
    meta http-equiv="Content-Script-Type" content="text/javascript">
    <
    script language="JavaScript" type="text/JavaScript">
    function 
    createExternal(){
    var 
    root document.getElementsByTagName('head')[0];
    var 
    oJs document.createElement('script');
    oJs.setAttribute('type','text/JavaScript');
    oJs.setAttribute('src','myscript.js');
    root.appendChild(oJs);
    }
    </script>
    </head>
    <body>
    <input name="" type="button" value="fire the function" onclick="createExternal();bla()">
    </body>
    </html> 
    In myscript.js I have a simple function

    PHP Code:
    function bla(){
    alert('foo')

    It works ok in IE, But Moz says thare is no function bla(). Now if I change a little bit the codes, everithing works ok:
    PHP Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  "http://www.w3.org/TR/html4/loose.dtd">
    <
    html>
    <
    head>
    <
    title>Untitled Document</title>
    <
    meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <
    meta http-equiv="Content-Style-Type" content="text/css">
    <
    meta http-equiv="Content-Script-Type" content="text/javascript">
    <
    script language="JavaScript" type="text/JavaScript">
    function 
    createExternal(){
    var 
    root document.getElementsByTagName('head')[0];
    var 
    oJs document.createElement('script');
    oJs.setAttribute('type','text/JavaScript');
    oJs.setAttribute('src','s.js');
    root.appendChild(oJs);
    }
    function 
    alertV(v){
    alert(v)
    }
    </script>
    </head>
    <body>
    <input name="" type="button" value="fire the function" onclick="createExternal()">
    </body>
    </html> 
    and in external file:
    PHP Code:
    alertV('foo'
    I would like to know why Moz acts like that? is in the first variant incorrect? Or it is a Moz's bug?...
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #2
    Regular Coder
    Join Date
    Feb 2005
    Posts
    400
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I don't know the why...

    But, it's a matter of timing and thread priority. Take your first example and change the onclick function to "createExternal();setTimeout('bla()',0)" The document-object just needs a moment in which to adjust itself to the new script's presence.

    Another fun one is
    Code:
    var time, count=0;
    function blaReady(){
      if (typeof(bla)=='undefined'){
        count++;
        setTimeout("blaload()",0);
        return;
        }
      alert(new Date()-time+'ms\n'+count);
      }
    with an onclick of "createExternal();time=new Date();blaReady();"

  • #3
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Yeap, I guessed that it must be a timing problem... It looks like Moz is a little bit more anxious to execute the next function before have had "swallowed" all the previous code lines... It looks a bug to me...
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #4
    Regular Coder
    Join Date
    Feb 2005
    Posts
    400
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yeah, the DOM should update itself with a higher priority than the in-page scripts. If you put a while loop in the onclick to test whether bla is available, it never will be (well, not until the onclick script times-outs out and the browser lets you kill it).

    onload is good thing, I guess. Speak of which, how would you build an onload function using setAttribute? oJs.onload=function(){bla()} works but isn't pretty.

  • #5
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    actually I use onload, as I need to create a certain SRC for script for each page. Those js pages are very short, in fact there are simple arrays with some variables (number of variants for some items) which are to be created by a server application from database. I have manage to solve the problem as I said, by continuing the main code lines in the main page using a function inserted in that small external js. Thus Moz has eneough time to reconsiders it's new created element and only after that the following code lines are to be continued

    function bla()
    ...
    ...
    //creates the SCRIPT element
    //its SRC is set according to some parameter in the location (the page loaded)
    }
    function continuebla(parameters){
    .../the other code lines which needs those parameters
    }

    and the external js files have something like(one of them)

    var parameters = new Array(10,3,5,7,12,0,3)
    continuebla(parameters)

    It might not be the most elegant solution, but it works.

    So that, as I have previousely said, the problem is already solved, but I was just wondering wether that is or not a Moz bug...
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #6
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Quote Originally Posted by Kor
    I would like to know why Moz acts like that? is in the first variant incorrect? Or it is a Moz's bug?...
    It's not a moz bug. It's a logic error on your part. Pure JavaScript execution is, with the exception of timeouts/intervals, synchronous - everything happens in a sequence, and the next statement will be executed as soon as the last finished. But when you include scripts using the DOM you introduce asynchrony to JavaScript - the script element will be written to the DOM synchronously and then the next JavaScript statement will be run. But the loading of the script is asynchronous (takes place while the script is running) and the loadded script will be sent to the scripting engine when fully loaded, independent of how long the currently runnung script have progressed by then. The single threaded scripting engine will execute the script when it has turned idle at some point, which means it will finish any currently running script first - very similar to how it works with all other asynchronous elements of JavaScript: timeouts/intervals, events and asynchronous loads á la XMLHttpReq or doc.load.

    In other words, you're assuming that execution of loaded scripts are either instantaneous or synchronous, which it isn't.
    Last edited by liorean; 04-07-2005 at 02:35 PM.
    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


  •  

    Posting Permissions

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