1

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?

4
  • 1
    You really can't. If the code is imported into your page, it can do whatever it wants. You could keep third-party stuff in separate <iframe> blocks maybe. Commented Jan 12, 2016 at 14:21
  • 2
    i dont think you can stop the APIs to load jquery. try using api.jquery.com/jquery.noconflict Commented Jan 12, 2016 at 14:24
  • 1
    If its only about conflicts, then you can make use of $.noConflict(true) before the third party script tag. Commented Jan 12, 2016 at 14:25
  • 1
    This might help : stackoverflow.com/questions/1566595/… Commented Jan 12, 2016 at 14:26

2 Answers 2

3

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.

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

1 Comment

Agree with this. The third party's absence of noConflict is a mistake.
3

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.

Comments

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.