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');
script.type = 'text/javascript'; script.src = 'code.js';
document.getElementsByTagName('head')[0].appendChild(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
}
}

4 comments:

Anonymous said...

Hi! I would like to ask if there is a solution that works also for outlook clients. I mean: exists a directory where i could put my javascripts so that they are also available (get synchronized or whatever) by outlook clients without any additional effort for me?

website design said...

Never knew that tricky way is possible, but in some cases, I agree on your tricks. Its kinda cool.
website design

Anonymous said...

Hey Ronald,

could you tell me script to retrieve entity id (not the one on which javascript gets triggered) usine REST endpoint.
To explain it in better words, I want to retrieve Records of a custom entity and perform some condition operations on them and update current contact record.

Thanks in advance

Anonymous said...

Setelah mencoba bertanya kepada orang-orang akhirnya dia menemukan OBAT KLAMIDIA yang ampuh dengan khasiat alami. Obat klamidia ini merupakan obat yang seratus persen menggunakan bahan yang diambil dari alam. Silahkan baca artikel selengkapnya. Obat alat vital bernanah secara alami ini lain daripada yang lain. Pengobatan ini berbeda dengan pengobatan pada umumnya. Karena kita ketahui sendiri pengobatan ini lebih menekankan dengan penggunaan bahan-bahan alami yang seratus persen berasal dari alam. Silahkan baca artikel selengkapnya. Ambeien dapat disembuhkan dengan mengonsumsi Obat ambeien , aman, halal, dan terjangkau yang bernama ambeclear. Tanaman Herbal yang bernama tanaman daun ungu merupakan bahan utama dari ambeclear. Silahkan baca artikel selengkapnya . Jika OBAT KUTIL KELAMIN medis ini ternyata juga tidak memberi kesembuhan, maka pengobatan alternative yang bisa menjadi pilihan adalah Obat kutil kelamin. OBAT HERBAL KUTIL KELAMIN ini lebih alami dan tanpa efek samping seperti pengobatan kimia lainya. Silahkan baca artikel selengkapnya