First off, how to define a function.
There are actually several different ways, for instance the normal:
function myfunc1(param1, param2)
And the one with a weird sort of syntax:
var myfunc2 = function(param1, param2)
In CRM 3, these can actually be defined within the onLoad, hence creating some ways of code reuse. These functions will be available for all event in the page since they have been declared in the onload.
This leads me to another point, that a function has to be defined before it can be used. Hence, in the example above, myfunc2 can call myfunc1, but not the other way around. If you try, you will get strange error messages saying stuff like “null is not ‘null'”.
First the simple method by just setting the onchange attribute.
crmForm.all.ext_myfield.onchange = myfunc2;
document.getElementById(“objectid”).onchange = myfunc2;
This has the advantage of a quite simple syntax and, if you want it, the override of previous events.
The second way of doing this is using the attachEvent method. As far as I’ve read, there are some differences with syntax between browsers, but since neither MS CRM nor SharePoint work in any acceptable way in anything but IE, this is not a big issue. Here is some example syntax:
The main advantage of this method is that you can attach several functions to one event, hence, not overriding other handling.
Bellow is an example html-file which demonstrates this:
var myfunc1 = function()
var myfunc2 = function()
var loading = function()
If you copy this to a file called test.htm and test it, you will see that when clicking the button, myfunc2 will be executed first and then myfunc1.
So, all put together, this can be used to a great extent in MS CRM 3, where you can now put all code in one place, the onLoad script, define some functions and then bind the onchange event of all the attributes you like in the same code. Remember to define the functions before you attach the events.
Thank you so much!
I spent a good hour or so trying to find out how to bind an “onchange” event to a sharepoint field. Your code coupled with the _spBodyOnLoadFunctionNames.push(“FunctionNameGoesHere”); function (which you place inside the PlaceHolderMain area worked wonders!
Using that _sp function allowed me to load your “loader” function and bind several events to various fields.
Thank you again, really appreciate the article.
Yes, it is a very usefull technique. I use it a lot myselft but nowdays only in MS CRM.