0

I want to write simple javascript, something for collecting visitor info and the script needs JQuery to run.

Then I want to provide that script, so other websites can include/load it from me externaly, using script src="my_web/my_javascript.js"...

My dilemma is how to handle JQuery part, how to include JQuery, I don't know wheather the site that will include it has JQuery loaded or not, and if I force loading JQuery I don't want to cause conflicts or any problems on the site that uses my script.

4
  • Not a good idea. They might have an incompatible jquery version, either bundle your own jquery with noConflict or remove the dependency by just inlining the functions you use from it with some clean-ups. Commented Jan 28, 2014 at 16:36
  • Sorry didn't understood the last part, what do you mean by "remove dependency by inlining functions"? Commented Jan 28, 2014 at 20:54
  • Let's say you have $('#someId').html('<div></div'>'), you translate it to document.getElementById('someId').innerHTML = '<div></div'>'. This was a simple example. For more complex ones check jquery source and put the source directly into your code. But this solution is only good if you use a small subset of jquery. Commented Jan 29, 2014 at 10:54
  • You can check youmightnotneedjquery.com to see how to remove your jquery code. Commented Jan 31, 2014 at 2:22

2 Answers 2

1

Just include jquery with your script - concatenate the 2 together and minify.

Sign up to request clarification or add additional context in comments.

4 Comments

I see here 2 possible issues, first is, could there be conflict problems if the site already loaded some other JQuery version? and second, that looks to me like overkill, isn't that so? and yeah, thanks for posting..
jQuery has a noconflict mode designed for precisely this scenario - you'll have to leverage it so it doesn't conflict with any other jQuery copies that happen to load alongside it. It does seem like overkill, but it's far, far simpler and ultimately faster for the user; otherwise you load, check to see if jQuery has loaded, check to see if it's exactly the version you wrote against, generally you can't trust anything but the exact version you wrote for, so for nearly everyone you then delay your script and increase HTTP requests by fetching jquery afterall anyway... you get the idea.
I guess you are right. Although I guess I was expecting maybe some more elegant solution, as I see CMS sites full of all sort of plugins (unaware of each other), that use jQuery, so it looked to me like a bad practice to bundle in each plugin/script it's own jQuery library.
If you don't control the page itself loading the scripts, you're already too late to save the user any time in terms of what scripts will be loaded. CMS's do control the page so they can do what you observed.
1

You can detect if jQuery is loaded at the beginning of your script, to warn the user to include it if it isn't.

if (typeof jQuery == 'undefined') {  
    alert("jQuery is required!");
}

3 Comments

This solution looks fine, check for jQuery and if not present I could load it inside my code, asking the user to add my script before closing the body tag. Couldn't make problems for any other javascript on the page, right?
jQuery could conflict with other javascript libraries on the page. But they also have a "noConflict" mode for that.
The downside here is now you have to go load an external script and there's no jQuery to help you. You're increasing code size to... avoid the risk of increasing code size? At the additional cost of an extra HTTP request. Just include jQuery and be done with it. It seems excessive but that same design philosophy got us Windows DLL hell.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.