web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Dynamics 365 Community / Blogs / Nishant Rana’s Weblog / Loading External JavaScript...

Loading External JavaScript files

Nishant Rana Profile Picture Nishant Rana 11,325 Microsoft Employee

Suppose this is our external javascript file content.

(customscript.js)

It consist of a simple function

function SayHello()
{
    alert(‘Hello World’);

}


 

It is placed at the following path   ..\ISV\ExtJS. i.e. within ExtJS folder inside ISV.

Place the following code in the form load of an entity’s form.

var scriptElement=document.createElement("<script type=’text/javascript’>");

scriptElement.src="/isv/ExtJs/customscript.js";

document.getElementsByTagName("head")[0].insertAdjacentElement("beforeEnd",scriptElement);

SayHello();

At times, it would work properly however sometimes it would throw “Object expected” error. It is because browser load JavaScript files asynchronously. If the script isn’t loaded we would get the error.

So here we need to make use of onreadystatechange event.

So we would modify our script as following

 

var scriptElement=document.createElement("<script type=’text/javascript’>");

scriptElement.src="/isv/ExtJs/customscript.js";

scriptElement.attachEvent("onreadystatechange",loadScript);

document.getElementsByTagName("head")[0].insertAdjacentElement("beforeEnd",scriptElement);

function loadScript()

{

if(event.srcElement.readyState=="loaded" || event.srcElement.readState=="complete")

{

    SayHello();

}

}

 

 

Or we could also using XmlHttp request object to load the external file.

var url="/isv/ExtJs/customscript.js";

var xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");

xmlHttp.open("GET",url,false);

xmlHttp.send();

eval(xmlHttp.responseText);

SayHello();

 

Bye..


Filed under: CRM, Microsoft Dynamics CRM Tagged: CRM, CRM 4.0

This was originally posted here.

Comments

*This post is locked for comments