How to load Javascript from a webresource

Imagine an usecase where you can dynamically load a Javascript from the CRM webresources because it doesn’t need to be loaded on every single form load. Perhaps a polyfill to add missing browser functions or a Javascript library like jQuery. Here it is…

My personal usecase was to add a promises polyfill to the Internet Explorer. So, if you combine this post, my post about “Internet Explorer and promises” and you have a promises polyfill in your CRM webresources, you can use promises and do only load the polyfill in case the browser doesn’t support it.

 


Limits for custom parameters

Have you ever asked how many characters you can pass as parameter to a Javascript event handler or to a HTML webresource? I do, because I like to give a consultant or the customer the possibility to pass dynamic values in form of JSON objects or even whole functions without touching my code.

HTML webresource

The limit here is at 1.500 characters.

Javascript event handler

I think there no limit. Really. I made a test with up to 2.400.000 characters without any problems. The only thing that I would mention – do not exaggerate it as I did, because the performance suffers quite a lot.

 


Pass parameters to HTML Webresource

A HTML webresource can not just only show HTML elements on your form, furthermore you can work with JavaScript in it and interact with your form. IF you want to access the Xrm namespace of the form inside your HTML webresource, you simply need prepend “window.parent” to your function call.

This looks like in this example:

If you need a lot of such calls, you can make your life easier and create your own local variable and assign it the complete Xrm namespace
This could look like this:

Pass parameters to HTML Webresource

Alternatively, you can also pass static values to your webresource. Just add a custom parameter in properties dialog of the webresource.

Pass parameters to HTML Webresource

You can access the custom and contaxt parameters with the following script inside your webresource:

The custom parameters should be in the form “Name=Value” should be separated by comma.

The debugger shows you all the accessible parameters:

Pass parameters to HTML Webresource

Context parameters are green marked, custom parameters blue.

You can now access your values through the named array.

Have fun with it!