I got struck by a curious problem the other day. What I was doing was trying to get some Javascript code which another member of my team had written to work in Titanium, but it just was not willing to work. I did discover that the problem occurred in Safari too, so the problem was with Webkit, and not Titanium.

After two very frustrating days fighting with the code, I finally figured out what the problem was. Firefox and Internet Explorer load Javascript pages consecutively, whereas, Webkit loads them asynchronously.

what this means is that in Firefox and IE, you can be sure that the previous script will be fully loaded (with all the functions and variables defined) before the next script is loaded, thus allowing the data to be accessed from the second script. With Webkit, there is no such guarantee. The problem I had encountered, is that a few objects that were defined in an earlier script were not yet loaded by the time they were needed in the later script, which caused everything to come crashing down.

So, as an example

<html>
<head>
<script language="Javascript" src="a.js"></script>
<script language="Javascript" src="b.js"></script>
</head>
</html>

would load the two scripts

a.js

var a = "Hello World";

function test() {
   return "Testing";
}

b.js

alert(a);
alert(test());

The code above would work with no problems in IE and Firefox, but there is no guarantee it would work in Webkit (and by implication Safari).

So, how would you ensure the code works? Well, there are two options. You could either ensure there were no dependencies by making sure the scripts are self-contained, or you could use a utility like JSMin which is a PHP script which combines the individual scripts into one file and then sends that to the browser.

Share