03 Nov 2011, 14:40

Browser specific Javascript loading with jquery.loadScriptForBrowser.js

It’s not very often that I write Javascript that needs to be downloaded and run by only one browser, but when I have to, I want it to be easy. I don’t want to waste a bunch of time doing user agent parsing and checking, I want to just write what I need for that specific browser, and go on my way.

A quick look through the Googles didn’t really yield anything that I could use, so I threw together a jQuery plugin; jquery.loadScriptForBrowser.js. The browser detection functionality & logic is from the excellent jQuery Browser Plugin (which I’ve used many, many times before to allow me to target CSS to specific browsers without having to resort to difficult to read, maintain, and understand hacks). Just as the code from the jQuery Browser Plugin, this plugin is MIT licensed.

Sample usage for this plugin:

<script type="text/javascript" src="./js/jquery.loadScriptForBrowser.min.js"></script>
<script type="text/javascript">
    $.loadScriptForBrowser({
        'chrome': [
            './js/chrome.specific.js'
        ],
        'msie': [
            function(){console.log('Browser: MSIE')}
        ]
    });
</script>

Not much to it. You can check it out at http://git.sdb.cc/projects/jquery-loadscriptforbrowser-js, or just go ahead and clone the repository: git clone git://git.sdb.cc/jquery-loadscriptforbrowser-js.

comments powered by Disqus