My Cart (0)  |  My Orders  |  My Downloads  |  My Auction  |  My Account  |  Help


Login |Register        Search

Jquery in dnn overview

                Print      Add To Favorite     Add To Watch List     Contact Author

Creator: host   6/4/2012 3:01:18 AM    Author: Ian Robinson   Source: http://www.dotnetnuke.com/Resources/Blogs/EntryId/3320/DotNetNuke-and-jQuery-An-Overview.aspx   Views: 3170    0    0  
Tags:
Module develop Jquery

by Ian Robinson on Thursday, March 08, 2012 6:44 PM
  • Recently I received an email from someone who had attended one of my DNN training sessions asking to clarify some questions about jQuery usage. This person was just getting started with jQuery in DNN, and had inherited a skin that loaded jQuery itself, but they also realized that DNN was loading jQuery as well. They noted that everything seemed to be working fine, but were a little apprehensive that one day things may grind to a halt.

Instead of just replying to the email, I’m going to answer these questions publicly in hopes that it benefits others.

How does DNN load jQuery?


Years ago (late 2008, I think), DNN 5.0 added direct support for loading jQuery. Since then, the right way to load jQuery from a skin or a module, is to ask DNN to register it for you. So think of it like this – every time DNN loads a page, it’s waiting for someone to say “I want jQuery!”. If nobody says that – it won’t be loaded. If one module or skin says that – it will be loaded once. If 20 modules ask for jQuery, it will still only be loaded once. Same goes for jQuery UI, which was introduced in DNN 6.

When jQuery or jQuery UI is loaded by DNN, the DNN options set in the Host settings are used:

image

So if DNN loads jQuery, and the skin/module loads jQuery, which is actually being used?

The answer to this one is quite simple. The last one to load will win. This is because both of the scripts will be defining the global jQuery object. So the second one will literally overwrite the first one.

Any potential issues upgrading DNN, or using DNN modules?

Sure! You could experience a wide variety of issues if jQuery is being loaded more than once. It is really quite unpredictable given the variety of ways people can organize/load JavaScript on a page in DNN, and the extent to which they do (jQuery is really common).

If you are loading two different versions of jQuery, you may have functionality that depends on a particular version. If for some reason that version is being loaded first and getting overwritten by another (presumably older) version, your functionality would likely just stop working.

Additionally, there are some interesting repercussions for plugins when jQuery is loaded more than once, as plugins just extend the jQuery object. For example:

  1. jQuery is loaded by DNN. The jQuery object is defined.
  2. A jQuery plugin extends the jQuery object with its functionality.
  3. A skin or module loads jQuery. The jQuery object is redefined, and its like the plugin never got loaded.

Why do some modules have settings for loading jQuery?

In an effort to support multiple versions of DNN, module developers that create modules which depend on jQuery will often give the administrator an option to opt out of loading jQuery. If you’re opting out, the module developer assumes that you’re loading it via some other mechanism, presumably through the core functionality in DNN.

What to do?

Try to only load jQuery once. This means using the API to register jQuery if you are developing a module or skin. It also means telling any module that has the option to *not* load jQuery itself, but to use DNN.

Resources

Want to register jQuery the right way? Check out these community resources:

If you have any questions or any more techniques/resources please post them in the comments.


Rating People: 10   Average Rating:     

     DnnModule.com is built to provide DNN quality modules and DNN skins, some of them are free, some not. We wish these stuffs (free or not ) can be useful to you.

     Besides that, we also provide a full range of professional services, ranging from web site build, seo, system management, administration, support, senior consultancy and security services. We act as if your development project or network was ours, with care and respect. We are not satisfied until it works the way you want it to, and we don't silently ignore found issues as somebody else's problem.