Include Javascript files in CRM
This must be familiar to most of you: you're making quite some modifications to the onchange event of a picklist. To keep this readable, you do use tabs and breaks, but after saving this, all tabs and enters are gone. Therefore I like to put most code into .js files and include these files in the page. This would break the contract tough. Another possibility is the httpmodules, but they require quite some effort.
Now I was searching for some information regarding Javascript and I found some interesting codes. One of these could be used to solve the problem as described above! What about loading javascript files WITH javascript? It is possible by using this script:
var script = document.createElement('script');
When putting this code into the onchange event, this stays readable, does not cost much effort and you wont break the support contract!
Edit:
The script above could give troubles when a function call is made to the javascript file when it is not yet fully loaded. Thanks to Alex for providing this script:
function OnChange()
{
var script = document.createElement('script');
script.language = 'javascript';
script.src = 'MyFunctions.js';
document.getElementsByTagName('head')[0].appendChild(script);
var f = function()
{
if (event.srcElement.readyState == "loaded")
SomeFunction(); // some function from MyFunctions.js
}
script.attachEvent("onreadystatechange", f);
}
// function OnChange()
Update:
Just a bit better readable script:
var script = document.createElement('script');
script.language = 'javascript';
script.src = '/Custom/JavaScript/opportunityOnloadOnsave.js';
script.onreadystatechange = OnScriptReadyState;
document.getElementsByTagName('head')[0].appendChild(script);
function OnScriptReadyState()
{
if (event.srcElement.readyState == "complete")
{
//perform onload scripts
CustomOnLoad(); // some function from your custom js file
}
}