slayeroffice - web experiments gone horribly awry

09.26.2005 - Safari & createElement("script")

I was browsing through Safari's bugzilla and found this extremely exciting bit of information:

"Here's a very simple workaround. Set the onload and src attributes of the script *before* inserting it into the document:"

That's right - we can now get Safari to source scripts created with createElement! I have verified this to work in Safari 2.0.1 with MODIv2 and the original MODI.

The favelets themselves dont work terribly well (yet -- which is why I haven't updated the links), but that's beside the point. This is wonderful news - and not just for favelets either. This is gonna be a great boon for web development as a whole.

Just think - no longer do you have to trudge in a slew of scripts that you might not even need -- now you can dynamically pull in scripts when the functionality they provide is requested without having to worry about Safari!. Oh, happy day.

For those interested, the actual change for MODIv1 is as follows:

The Old Way:

The New Way:

Time permitting, a progression of updates will start coming out for the favelets over the coming weeks.

Nice spotting :).

Just one trick I thought I'd suggest -- I picked this up myself recently. For favelets, try instantiating and running a new anonymous function like this:

javascript:(function(){ var abc = 123; foobar(abc); whatever(); })();

That removes the need for ensuring that the last statement is void and also allows you to use "var" with impunity, as it's locally scoped.
Posted by Angus Turnbull on September 26, 2005 @ 8:14 pm

Just curious, is the language property actually needed? AFAIK it doesn't mean anything to modern browsers.
Posted by Stephen Clay on September 26, 2005 @ 8:50 pm
@Angus: I'd seen that done in GM scripts - it hadnt occured to me to use that method on a favelet. Nice.

@Stephen: Nah, its not. Just one of those legacy things from years ago that proliferated through my lazy copy/pasting :)
Posted by Steve on September 26, 2005 @ 11:52 pm

Setting attributes before inserting the node into the document tree is faster than setting the attributes afterwards. So even if it works the other way round, one should do it in this order. I don't know how much faster it is, though.
Posted by Martin on October 12, 2005 @ 6:14 pm
What about Omniweb and Konqueror? Will this apply to them too since these browsers cannot append Js as well?
Posted by Jo on October 16, 2005 @ 6:28 am
Ohh, nice!

I was gonna get to work figuring out why my script wasn't working in Safari tomorrow, so you've saved me some time. :) Thanks alot. :D I'm terrible at bugtesting on Mac's, just can't comprehend em, you know.
Posted by Jonas Pihlström on November 10, 2005 @ 4:39 pm

That's good. But i have a question, if the src script had a "document.write" open a new window on firefox, but IE, Opera and others works fine. Do you know why?

For example, i try load this , on IE, Opera works fine, but Firefox open a new window.

Best regards

Posted by Mario Brhemenz on November 16, 2005 @ 6:46 pm

Did you actually test "the workaround"? I tried it and it doesn't fire onload event. (Safari 2.0.2) :-(


----- cut here ----

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE html

PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"




<title>Hello World from External Javascript</title>

<script type="text/javascript">


oScript = document.createElement('script');

oScript.type = 'text/javascript';

oScript.src = 'test.js';

oScript.onload = oScript.onreadystatechange = scriptLoaded;


function scriptLoaded() {






<h1>Hello World Example</h1>

Did we get "Hello world" alert?



----- cut here ----


----- cut here ----

function helloWorld() {

alert("Hello World!");


----- cut here ----
Posted by Boris on November 17, 2005 @ 6:39 am

Of course I "actually tested it" - I wouldn't have posted about it if I hadn't.

Take the "new way" code snippet in the example and use that. I cant see why you would need to fire a method from the onload event of a script object anyway (i half suspect thats the issue as a few quick experiments shows Safari to not fire that event) - just call your init method inline from the bottom of the script if you need a function executed onload.

Go here:

And paste this in:

x = document.createElement("script");

x.type = "text/javascript";

x.src = "";

x.onload = function() { alert("woot!"); }


j = setOldTitles;

Firefox will alert, but Safari won't. Both source the script and write out the function from page_methods_v4.js - so, it works.
Posted by Steve on November 17, 2005 @ 8:34 am

Yes, it is about onLoad. It is not triggered after script is loaded.

And no, you can't call it manually ("inline" as you say) at the bottom of the script, because loading of the external script is asynchronous operation. If, in my example, I would add




helloWorld function would not be called because it is still not existing (error would be reported in Javascript console)

Posted by Boris on November 18, 2005 @ 6:39 am

I didnt mean call it from the first script, I meant call it from the bottom of the script you are loading like i do in nearly all of my favelets.

Have a look at any of the source of any of them that have an init() method and it'll be called as the last line in the script. No need to attach it to an event.
Posted by Steve on November 18, 2005 @ 9:11 am

I see now what do you mean. Seems fine, though I would be nice if onload would work.

Nice tool this MODI bookmarklet, great idea, fine job!

Posted by Boris on November 18, 2005 @ 11:41 am

I've just compiled it, end current development ("nightly build") version of Safari does trigger onload event. Nice!
Posted by Boris on November 21, 2005 @ 5:05 am
Ah, very cool!
Posted by Steve on November 21, 2005 @ 8:36 am
Here's my take on making a compact favelet/bookmarklet with an external source...









Posted by ca_redwards on November 23, 2005 @ 5:50 pm

Comments have been closed for this post.