I’ve got some javascript that I want to attach to forms and to HTML web resources in MS dynamics CRM 2011. It needs the Xrm object, which is available either directly when in a form context or via window.top.Xrm when in a HTML web resource.
I thought I’d write a quick function to check if Xrm is defined or not, allowing me to work out whether I’m running in the form or in a HTML web resource. As a starting point, because I’m really new to javascript, I wrote this code to check for Xrm being present and show a message.
alert('Running');
if(Xrm) { alert('Yup'); } else { alert('Nope'); }
alert('Done');
However, this doesn’t work and I don’t understand why. When it runs within a form event, such as form load, Xrm is present and I see the alerts “Running”, “Yup” and “Done”. However, when running in a HTML web resource, where Xrm is not directly available, I see the “Running” alert but the javascript just stops on the next line.
Please help!
When
Xrmis not defined your code will raise an exceptionReferenceError: Xrm is not definedand halt the execution of the rest of your Javascript. To check for the existence of a variable you should use: