Tip #628: Get jQuery the hacker’s way

The official word is:

We do not recommend using jQuery in form scripts and ribbon commands.

Right. And what are we to do with thousands lines of code already relying on that miracle? People insert libraries and keep their fingers crossed or follow the wise ones and encapsulate the instance with jQuery.noConflict() call.

There is an easier way, though. Since CRM uses jQuery itself, you can try doing something as simple as:

if (typeof ($) == 'undefined') {
  if(!!parent) {
    $ = parent.$;
    jQuery = parent.jQuery;
  }
}

Brutal but Works on My CRM™

Note: MSDN warns that “Following Microsoft Dynamics CRM Online 2015 Update 1 … there may not be an instance of jQuery available” but I’m yet to see any evidence of that.

The usual warning: contains small parts, use at your own risk and do not operate boats or heavy machinery.

7 thoughts on “Tip #628: Get jQuery the hacker’s way

  1. Philip Verlinden says:

    Or how not to create dependencies that could break…. 😉

  2. Stefan Schneider says:

    When you use TurbForms you will not have the default jquery2 in the onload event for example. The ClientApiWrapper has only Xrm (and Mscrm) and stuff like that.
    Speaking of 2016 here btw.

  3. Stefan Schneider says:

    Also, if you follow the official links you should get jQuery from other sources, either included in your formscript or with a scriptload or even from CDN

    https://msdn.microsoft.com/en-us/library/gg328261.aspx

    • In a perfect world, there is no need for jQuery in form script. The only place where it belongs is html web resource where jQuery can be loaded as “normal” script. The above is what it is – a hack.

  4. Stefan Schneider says:

    If you know thinking of adding your libraries in the form just before your form script. TurboForms produce race conditions so cannot be sure the libraries are always loaded before you use them.

Leave a Reply

Your email address will not be published. Required fields are marked *