Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to embed/import jQuery into another JavaScript library without causing potential clashes with jQuery/others deployed on the website?
    primarykey
    data
    text
    <p>I am working on a JavaScript library that makes internal use of jQuery. However, when the time comes for its deployment, I would prefer to simplify things (w.r.t. deployment) and not worry about whether the environment has (or doesn't have) jQuery already included in <code>&lt;head&gt;</code>.</p> <p>I should mention that my library is implemented as a class in Coffescript and the resulting .js file has the default function wrapper to isolate scope.</p> <p>I can think of a few solutions to this problem:</p> <ol> <li><p>Ask the website admin whether they have jQuery already present. If yes, just include the library itself. If not, then include jQuery + the library, perhaps in a single file and concatenated. Deployment is harder, but problem is likely solved.</p></li> <li><p>Create a file that concatenates jQuery and my lib code and always have the website admins just include this file. To prevent clashes, do a <code>var myJ = jQuery.noConflict()</code> right after the jQuery code.</p></li> <li><p>Do as in (2) above, but call the <code>var myJ = jQuery.noConflict()</code> inside the encapsulated Coffeescript class.</p></li> </ol> <p>In summary, the objectives for this challenge are:</p> <ol> <li>Absolutely avoid any breakage on the deployed website, regardless of what they might be using </li> <li>Keep deployment as simple as possible</li> <li>Avoid unnecessary transfer of jQuery, if possible</li> </ol> <p>My concerns are:</p> <ul> <li>What if the site already has jQuery, but needs an older/newer version? </li> <li>What if my <code>noConflict()</code> call breaks something on their end?</li> </ul> <p>What is the best way to tackle this situation? Am I missing something?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload