Skip to content

Instantly share code, notes, and snippets.

@johtso
Created July 7, 2011 21:33
Show Gist options
  • Save johtso/1070596 to your computer and use it in GitHub Desktop.
Save johtso/1070596 to your computer and use it in GitHub Desktop.
Load jQuery/jQuery UI dependencies
loadDependencies = (callback) ->
requirements = {jq: '1.6.1', jqui: '1.8.7'}
getScript = (url, next) ->
script = document.createElement('script')
script.src = url
head = document.documentElement.childNodes[0]
script.onload = script.onreadystatechange = onScriptLoad script, next
head.appendChild script
onScriptLoad = (script, next) ->
->
return unless script.onload
if !@readyState or @readyState is 'loaded' or @readyState is 'complete'
next()
script.onload = script.onreadystatechange = null
head.removeChild script
if not ($ = window.jQuery) or requirements.jq > $.fn.jquery
getScript "http://ajax.googleapis.com/ajax/libs/jquery/#{requirements.jq}/jquery.js", ->
getScript "http://ajax.googleapis.com/ajax/libs/jqueryui/#{requirements.jqui}/jquery-ui.js", ->
callback window.jQuery.noConflict(1)
else
if not (jqui_version = window.jQuery.ui.version) or requirements.jqui > jqui_version
getScript "http://ajax.googleapis.com/ajax/libs/jqueryui/#{requirements.jqui}/jquery-ui.js", ->
callback window.jQuery.noConflict(1)
else
callback window.jQuery.noConflict(1)
loadDependencies ($) ->
# Your code goes here:
alert "jq: #{$.fn.jquery}, jqui: #{$.ui.version}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment