Safari JavaScript Oddities

August 21st, 2005

One of the pieces of technology that we manage up here in comScore’s Toronto office is SiteRecruit, our website visitor recruitment software. Recently, despite the copious accolades, we discovered that parts of the software were not working correctly on the Mac when using Safari.

SiteRecruit is a fairly serious piece of software, as far as JavaScript software goes, and because we run on such high traffic sites it’s important that we’re as lightweight as possible, so one of the tricks that we use is to do a just-in-time script include, by dynamically adding a script tag to the document.

document.write('<script src="foo.js" />')

Safari seems to handle that fine. What it doesn’t deal with, however, is dynamically adding JavaScript code to the document itself, either through the DOM directly:

var body = document.getElementsByTagName('body')[0];
var script = document.createElement('script');
script.setAttribute('type', 'text/javascript');
script.setAttribute('language', 'JavaScript');
var someText = document.createTextNode('alert("foo")');
script.appendChild(someText);
body.appendChild(script);

Or by simply writing to the document:

document.write('<script>alert("foo")</script>')

Neither gets parsed or executed correctly. It’s a funny little corner of the browser to be working in. I wonder whether there are rules or specifications around adding inline JavaScript content to a document dynamically? My guess is that it’s a grey area, and the fact that you can do it in IE and Firefox is more a happy coincidence of the particular implementation than a conscious design decision.

So if you’ve landed here on the tail end of a Google search and are experiencing the same problem, apart from ending your misery quickly, I can’t help you. Luckily for us we generate our JavaScript programmatically with a tool that we’ve written (pictured above), so with a little effort we were able to factor some of the code that was being written dynamically into templates that end up as static JavaScript files.

Oh, and while I’m at it I have another odd Safari tidbit. The window object in pages opened with a target of _blank have no opener reference, so scripting between pages becomes difficult. A work-around is to open pages with window.open inside an event handler (so it doesn’t get blocked by popup blockers), which sets the opener correctly.

Note: If you’re reading this in an aggregator somewhere it’s likely that some of the content in the code examples above was stripped out. Writing about writing dynamic markup within markup that is getting XMLized and escaped all over the place is very meta and complicated. If you’re interested, just come and read on the site.

One Response to “Safari JavaScript Oddities”

  1. imran Says:

    Hi

    I did like your blog postings and comments but i have soem problem if some1 could help me out.

    Does anyone know how to pass html table data from parent to a child form html table ? Or may be you could let me know any URL related to this topic.

    Any kind of help is highly appericiated.

    Thanks

    Imran Hashmi
    http://www.visionstudio.co.uk