user217648
user217648

Reputation: 3466

prevent third party JavaScript to insert jQuery

We have a page which conatins jQuery and third party JavaScript API. This api inserts jQuery dynamically during run-time into the page. It causes conflictions.

How can I prevent the third party API to add jQuery to the page?

Upvotes: 1

Views: 367

Answers (2)

mohamed-ibrahim
mohamed-ibrahim

Reputation: 11137

My answer should be applied by the them not you, first Rule for Third-Party Javascript is:

"You Don't own the website"

so if they want to use jQuery then they have to use jQuery noConflict that is the best way to deal with a website working with multiple jQuery versions (Yours and theirs):

Completely move jQuery to a new namespace in another object.

var j = jQuery.noConflict(true);
// then you can say
j( "div p" ).hide();

if you add this line and then define a new jQuery then j will be an alias for old version and jQuery - $ will be an alias for the new version by default.

if you want to make sure; you can check the versions of jQuery present in your page:

j.fn.jquery      // this should show you your jQuery version 
jQuery.fn.jquery // this should show you their jQuery version

you can use it if you still need to use this third-party javascript and no way for them to change their code you can read more about jQuery noconflict

You can assign jQuery to a namespace you use as: mycode.$ and mycode.jQuery so you can later use:

mycode.$("div p")

this is always safer to use namespace to make sure no one else write in same code may override your variables.

Upvotes: 3

Steven Kaspar
Steven Kaspar

Reputation: 1187

There isn't really a way to stop a script from running that you pull in. I was dealing with something similar yesterday. There are ways to overwrite the changes it does obviously but there is no dynamic way to just stop it from running other than to just not pull it in.

Upvotes: 3

Related Questions