Prevent jQuery plural link

I am developing DNN modules, and I use jQuery in some modules, I add a jQuery link to the top of each ascx file, by the way, when a user adds several modules to the page that he links to every time the modules are added, this situation gives some errors. when I remove the link from the module that is at the bottom of another module that uses jQuery, they work well, is there a way to prevent multiple reference to jQuery? I mean if it is referenced on the page before it will not be referenced again. Thank you

+3
source share
3 answers

You can make a small script to check if jQuery is present on the page, and only if it is not there, download it:

// 'load-jquery.js'
window.onload = function () {
  if (window.jQuery === undefined) {
    var script = document.createElement('script');
    script.src = 'path/to/jquery.min.js';
    document.getElementsByTagName('head')[0].appendChild(script); // load jQuery
  }
};
+6
source

If you are using DNN 5.x, you must use the basic jQuery version for DNN when invoking DotNetNuke.Framework.jQuery.RequestRegistration().

If you are only dealing with a conflict in your own modules, you can add script code to the code and then check if it has already been added. We manually add jQuery to the header (create a general HTML control and add it to Page.Header.Controls), then call Page.ClientScript.RegisterClientScriptBlock()to create a script block to call jQuery.noConflict(so that it does not interfere with JavaScript DNN components). You can then wrap the entire call to add jQuery to the call Page.ClientScript.IsClientScriptBlockRegistered()so that it is added only once.

+3
source

jQuery , jQuery .

SOMETHING = {
    jQuery: jQuery,
    $: $
}

And then call jQuery through SOMETHING. $ or SOMETHING.jQuery. Just remember to do namespacing as soon as the jQuery version you want to download is downloaded, otherwise you will end up in the wrong version. If you have access to the jQuery file, the easiest way to do this is to add the namespace code at the end of the file.

+2
source

Source: https://habr.com/ru/post/1716471/


All Articles