Premise: I need to make a transparently reusable widget that uses jQuery and can work on any page (even if the page loads and uses its own version of jQuery); the widget will be loaded via a single <script="widget.js">
tag on the hosting page.
I am aware of the noConflict() proposed solution such as https://stackoverflow.com/a/528251/1710180
I have two concerns:
- as the widget itself is loaded via a JS tag, any
<script src="jquery.js">
tag it will insert in the document to load the jQuery library will be loaded async (AFAIK even if usingdocument.write()
); therefore, the 2-stepnoConflict()
resolution would be unreliable at best. - the widget also would need to load various jQuery plugins; once jQuery would be resolved without conflict to, say, a
jQuery_widget
global, how could I make these plugins bind againstjQuery_widget
as opposed tojQuery
?
Best solution I could come up with is compile and package the whole mess using an ES6 processor; that would at least fix me the jQuery name conflict in the global namespace; however, some of the plugins I would use are not ES6-ready so point 2) would still be not addressed by this solution since they won't use an ES6 import.
Another dirty solution that comes to mind would be to load everything in an iframe and use iframe.contentWindow.$
to manipulate the host window; I don't know how reliable jQuery would be cross-frame though, I have never tried something like this.