...

View Full Version : Moz weird dynamically behaviour



Kor
03-30-2005, 09:09 AM
<!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



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:


<!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:


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?...

Harry Armadillo
03-30-2005, 10:05 AM
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
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();"

Kor
03-30-2005, 10:10 AM
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...

Harry Armadillo
03-30-2005, 10:18 AM
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.

Kor
03-30-2005, 10:32 AM
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...

liorean
04-07-2005, 02:32 PM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum