Skip to content

Instantly share code, notes, and snippets.

@Acorn-zz
Created July 1, 2011 18:45
Show Gist options
  • Save Acorn-zz/1059155 to your computer and use it in GitHub Desktop.
Save Acorn-zz/1059155 to your computer and use it in GitHub Desktop.
Load jQuery and jQuery UI in bookmarklet
((window, document, requirements, callback) ->
getScript = (url, callback) ->
script = document.createElement('script')
script.src = url
head = document.documentElement.childNodes[0]
done = false
script.onload = script.onreadystatechange = ->
if not done and (not (readyState = @readyState) or readyState == 'loaded' or readyState == 'complete')
done = true
callback()
script.onload = script.onreadystatechange = null
head.removeChild script
head.appendChild script
if not ($ = window.jQuery) or requirements['jq'] > $.fn.jquery
getScript 'http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.js', ->
getScript 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.7/jquery-ui.js', ->
callback window.jQuery.noConflict(1)
else
if not (jqui_version = windows.jQuery.ui.version) or requirements['jqui'] > jqui_version
getScript 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.7/jquery-ui.js', ->
callback window.jQuery.noConflict(1)
else
callback window.jQuery.noConflict(1)
) window, document, {jq: '1.6.1', jqui: '1.8.7'}, ($) ->
# Your code goes here:
alert "jq: #{$.fn.jquery}, jqui: #{$.ui.version}"
@andybak
Copy link

andybak commented Apr 22, 2012

Can you post a compiled version? I installed CoffeeScript and tried to compile it myself and got an error:

ReferenceError: window is not defined
at Object. (/Users/andybaker/test.coffee:37:6)
at Object. (/Users/andybaker/test.coffee:44:4)
at Module._compile (module.js:441:26)
at Object.run (/usr/local/lib/node_modules/coffee-script/lib/coffee-script/coffee-script.js:79:25)
at /usr/local/lib/node_modules/coffee-script/lib/coffee-script/command.js:177:29
at /usr/local/lib/node_modules/coffee-script/lib/coffee-script/command.js:152:18
at [object Object]. (fs.js:123:5)
at [object Object].emit (events.js:64:17)
at Object.oncomplete (fs.js:1181:12)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment