While it’s sometimes necessary to call external JavaScripts, there seems to be an absence in guidance on how best to perform this task. I looked into this a while ago when writing the Google Tracker Extension where I checked if the session was SSL or not. This works fine and I don’t suspect I will change the extension.
That being said, today I ran across some code today that showed me another way to skin this cat… I figured I would share with everyone and get some feedback on whether one way is better than another.
So for reference, here is how my Google Tracker Extension handles the Google JavaScript call today:
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
And this is the code I ran across today:
<script type="text/javascript">
document.write([
"\<script src='",
("https:" == document.location.protocol) ? "https://ssl." : "http://www.",
"google-analytics.com/ga.js' type='text/javascript'>\<\/script>"
].join(''));
</script>
The second way seems more technically impressive, but I don’t see how the end result is any different. Does anyone have an input on whether one is better than the other? Or does it even matter?




Thu, Oct 16, 2008
Technology