Because scripts loaded in this way are loaded asynchronously there is the possiblity / probability that if calls are made to functions loaded within the library immediately the function may not yet be available (because the code is stil being laoded).
After fiddling with this I have come upon the following solution. This material is an adaptaion/combination of material I found here on the MSDN dev forum and also http://unixpapa.com.js/dyna.html. The post on the forum by itself doesn't cater for the varoius browser anomolies but was a great place to get started.
Solution:
1. At the bottom of the js library file place the function call pageLoad(); (assuming that this is NOT defined in the library).
2. In the onLoad event for the CRM form add the following code at the BOTTOM of the form
//----------------------------------------------------------------------------------------------
pageLoad = function() {
//now we do the stuff that includes stuff from within the loaded js page
//----------------------------------------------------------------------------------------------
// the code you want to execute once the page has loaded.
//This can safely call functions in the library
};
//=================================================
//Script for including external JS files
//The last line of this includes a call to the pageLoad function
//=================================================
var script = document.createElement('script');
script.language = 'javascript';
script.src = '/CustomDevelopment/jsLibraries/FormActions.js?';
document.getElementsByTagName('head')[0].appendChild(script);
This technique assumes you are able to edit the js library files. The js Library file in this case has been to a directory Customdevelopment/js Libraries located off the root of the web server directory.
The js library file has as its last line the function call pageLoad().
The js library is attached as the last section of the onLoad code. The reason for doing this is because Javascript libraries are loaded asyncronously. By placing the inclusion at the bottom of the onLoad code and placing the function call at the bottom of the js file it can be known that all functions defined in the onLoad code and in the library will be available.
Caveats:
I am pretty sure that this technique will not work well with the Outlook client and definetly not when it is offline.
My suggestion is that this could be used in a dev environment and then the functions cut and pasted from the library file into the top of the forms if needed in the production environment. It should at least make the dev process more standardised and this should help contribute to better codig etc :-)
It definietly helps to have a standard set of functions that you know work and are all the same.
No comments:
Post a Comment