Load JavaScript WebResource at runtime

Update... added Browser-agnosticity


function LoadWebResource(resource)
{
   var httpRequest = _createXMLHttpRequest();
   httpRequest.open("GET", Xrm.Page.context.getServerUrl() + "/webresources/" + resource, false);
   httpRequest.send(null);
   try {
      eval(httpRequest.responseText);
   }
   catch (e) {
      alert("Error loading " + resource + ":\n" + e.description);
   }
}

// Browser-agnostic factory function
function _createXMLHttpRequest()
{
   if (window.XMLHttpRequest) {
      return new XMLHttpRequest();
   } else if (window.ActiveXObject) {
      return new ActiveXObject('Microsoft.XMLHTTP')
   } else {
      _error("Could not create XMLHttpRequest on this browser");
      return null;
   }
} 


------



function LoadWebResource(resource) {    var orgName = Xrm.Page.context.getOrgUniqueName();    var httpRequest = new ActiveXObject("Msxml2.XMLHTTP");    httpRequest.open("GET", "/" + orgName + "/webresources/" + resource, false);    httpRequest.send(null); try {       eval(httpRequest.responseText); } catch (e) {       alert("Error loading " + resource + ":\n" + e.description);    } }
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s