I’m writing a jQuery library that has a couple jQuery library dependencies, one being jQuery Cookie. What’s the best way to include this so that the users of my library use it?
I can think of at least three:
1 – don’t use jQuery Cookie, and instead include my own cookie utility functions
2 – include jQuery Cookie in my code, and use it unless it is already present on the page
3 – include jQuery Cookie as a separate file, and state that the users have to include jQuery Cookie to use my library
What’s the preferred method?
If what you’re doing with the cookies isn’t overly complex, you should probably write your own small helper function to achieve that. This would be the best option.
If you have to use the jQuery Cookie plugin, do not include it in your script. State it as a dependancy, and link to the jQuery cookie page. There many reasons for not including it in your plugin, but here are 2 that come to mind:
If the user already has that plugin loaded, including it again in your script will result in many extra bytes wasted for no good reason.
When the original author of the other plugin subsequently issues bugfixes to their code, you don’t want to constantly keep track of that and issue updates to your plugin.