Andrea Casaccia
Andrea Casaccia

Reputation: 4971

Can I load jQuery dynamically to a local variable rather than to window?

I am developing a widget to implement a "Zopim" like live chat. To modify the DOM of the page my widget will live in I'd like to use jQuery, but I want to do that without polluting the global namespace of the host page.

How can I achieve that?

Upvotes: 0

Views: 110

Answers (2)

charlietfl
charlietfl

Reputation: 171690

Steps

Check if window.jQuery is defined. If it is and matches version you need (jQuery.fn.jquery) use that version.

Otherwise Load jQuery and use noConflict(true) to assign to another variable and maintain any existing $ in page

Upvotes: 1

Gregor Menih
Gregor Menih

Reputation: 5126

Use jQuery.noConflict(true)

jQuery.noConflict(true) will remove all jQuery variables form global scope.

https://api.jquery.com/jquery.noconflict/

Upvotes: 1

Related Questions