javascript - 3rd party website service which requires JQuery -


i putting service offers tracking / analytics style functionality, , plan offer out users include on website can paste in reference external javascript file. javascript code relies on jquery far quicker, easier , more robust code in way unsure how best include reference on users site - of sites using service may have jquery, may not , have differing versions. there best practice here? options have come with:

  1. include minified jquery (possibly custom build functionality need) custom jquery alias in external js file
  2. load jquery external js file (again using custom jquery alias)
  3. use conditional loader such yepnopejs ensure jquery installed
  4. document requirement have jquery installed , leave user ensure there , compatible version

my concerns 3 future versions of jquery may incompatible (although unlikely it's core functionality being used). want avoid 4 users may not technical , issues may put them off using service altogether

is there further solution have missed?

you can use dirty trick - jquery has jsonp callback function named define, work amd. can use define callback jsonp callback run code depends on jquery:

<html> <head> <title>maybe jquery</title> </head> <body> <div id=status>jquery loading...</div>  <!--<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>--> <script> function jqueryready() {     // place on-jquery-load statements here     $('#status').text('jquery loaded.'); } if (!window.jquery) {     define = jqueryready;     define.amd = { jquery: true };      var script = document.createelement('script');     script.src = '//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js';     script.async = true;     var s = document.getelementsbytagname('script')[0];     s.parentnode.insertbefore(script, s); } else {     jqueryready(); } </script> </body> </html> 

demo: http://brass9.com/test/maybe-jquery/

since question says you're using on third-party sites have anything, i've modified answer ensure code (less) harm existing libraries if present on page - still harm libs require.js if require has loaded , jquery has not, asynchrony entirely possible. i'll update later resolve issue.

previous answer (doesn't work david noted):

<script> if (!window.jquery) {     var script = document.createelement('script');     script.src = '//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js';     var s = document.getelementsbytagname('script')[0];     s.parentnode.insertbefore(script, s); } </script> <script> // work jquery needed </script> 

Comments

Popular posts from this blog

Change php variable from jquery value using ajax (same page) -

Pull out data related to my apps from Android Play Store and iOS App Store -

How can I fetch data from a web server in an android application? -