This week I got an angry tweet from @peter_lind. He was complaining about the use of global variables in almost every tracking script. I would say ad-scripts are just as bad. If not worse. I mean take a look at this little bastard:
<!-- OAS SETUP begin -->
<script type="text/javascript">
<!--
//configuration
OAS_url = 'http://oasc.unt.se/RealMedia/ads/';
OAS_sitepage = 'unt/start';
OAS_listpos = 'Bottom1,Top,x80,x20,x60,Position1,Left1,Left2,Position3,Top2,Position2,x62,x01,x02,x03,x04,x05,x06,x07,x08,x09,x10,x11,x12,x83,Bottom';
// '';
OAS_query = 'keyword=' + escape(location.pathname + location.search);
OAS_target = '_top';
//end of configuration
OAS_version = 10;
OAS_rn = '001234567890'; OAS_rns = '1234567890';
OAS_rn = new String (Math.random()); OAS_rns = OAS_rn.substring (2, 11);
function OAS_NORMAL(pos) {
document.write('<a href="' + OAS_url + 'click_nx.ads/' + OAS_sitepage + '/1' + OAS_rns + '@' + OAS_listpos + '!' + pos + '?' + OAS_query + '" target=' + OAS_target + '>');
document.write('<img src="' + OAS_url + 'adstream_nx.ads/' + OAS_sitepage + '/1' + OAS_rns + '@' + OAS_listpos + '!' + pos + '?' + OAS_query + '" border="0" /></a>');
}
//-->
</script>
<script type="text/javascript">
<!--
OAS_version = 11;
if ((navigator.userAgent.indexOf('Mozilla/3') != -1) || (navigator.userAgent.indexOf('Mozilla/4.0 WebTV') != -1))
OAS_version = 10;
if (OAS_version >= 11)
document.write('<scr' + 'ipt type="text/javascript" src="' + OAS_url + 'adstream_mjx.ads/' + OAS_sitepage + '/1' + OAS_rns + '@' + OAS_listpos + '?' + OAS_query + '"><\/script>');//-->
</script><script type="text/javascript">
<!--
document.write('');
function OAS_AD(pos) {
if (OAS_version >= 11)
OAS_RICH(pos);
else
OAS_NORMAL(pos);
} //-->
</script>
<!-- OAS SETUP end -->
As you might have figured out I’ve taken this from unt.se. The Ad-company is a common one used on a lot of big sites. People complained about them already back in 2010. Just look at this blog-post by Rob.
Let me tell you a story. A little bit over a year ago I was working on a site where they insisted on using AddThis. I really don’t like this sort of scripts but I guess I save that rant for another post. On the site I was also doing a component using Raphaël. All good. Or so I thought. But if I downloaded the minified version on Raphaël and put it together with AddThis I got a conflict. They were both using global variables! Bummer… Since then both of them have updated their scripts. Maybe/Hopefully cause people like me emailed them and complained.
But why are big companies still using global variables? Only explanation I can think of is that they think, just like Google Analytics (with global variable _gaq) that they are big enough to “own” a global variable. Cause they can’t be so bad that they don’t know how to do it better, no?
So please people producing js for the public: Do not pollute the global namespace! And no, do not think that if you are big enough it’s ok. You will never be that big.