none
Replicating common script to all entities

    Question

  • Hi Friends,

    I have written java script, i want this java script to replicate to all enities, is there any easy way instead of copying to all entities,this java script will fire on onload of the page of all entities 

     

    Thanks in Advance 

     

    Regards,

    Kumar

     


    kumar
    Thursday, July 15, 2010 8:58 AM

Answers

  • To avoid modifying the DOM as the other examples show, as this is an unsupported approach to including scripts, you should use the following

    IncludeExternalScript = function(scriptFile) {
      try{
        var netRequest = new ActiveXObject("Msxml2.XMLHTTP");
        netRequest.open("GET", scriptFile, false);
        netRequest.send(null);
        eval(netRequest.responseText);
      }catch(ex)
      {
        alert('An error occurred setting up external scripts');
        return false;
      }

    return true;
    }

    if you do not want the script file to be cached then you can use  scriptFile + '?' + Math.random() as shown below instead.

    IncludeExternalScript = function(scriptFile) {
      try{
        var netRequest = new ActiveXObject("Msxml2.XMLHTTP");
        netRequest.open("GET", scriptFile + '?' + Math.random(), false);
        netRequest.send(null);
        eval(netRequest.responseText);
      }catch(ex)
      {
        alert('An error occurred setting up external scripts');
        return false;
      }

    return true;
    }

    This version of including scripts also makes the included script immediately available (eval) so you can just start calling your functions.


    MSCRM Bing'd - http://bingsoft.wordpress.com
    Friday, July 16, 2010 9:24 AM
    Moderator

All replies

  • Hi Anil,

    You can create a .JS file [containing the common code] and save the file in ISV folder.

    add the below code in ON LOAD of the entities where you need the common functionlaity. Eg your file name is "Test.Js"

    var oScript = document.createElement("<script src='/ISV/Test.js' language='JavaScript'>"); 
    document.getElementsByTagName("head")[0].insertAdjacentElement("beforeEnd", oScript);

    Once you add above code you can call your function which are available in .JS file.

    Hope this will help you.


    Thanks, Ranjitsingh R | http://mscrm-developer.blogspot.com/ | MS CRM Consultant
    • Proposed as answer by Ranjitsingh R Wednesday, August 04, 2010 8:19 AM
    Thursday, July 15, 2010 9:04 AM
  • you can create an external JS file and call these file at the time of load in you all entities,

    refer for the same

    http://mscrm4ever.blogspot.com/2008/08/referencing-external-js-file-from-crm.html

    https://community.dynamics.com/blogs/crmmattwittemann/comments/27787.aspx


    Mahain
    Thursday, July 15, 2010 9:06 AM
    Moderator
  • THanks for your reply  , i am looking not to call on all entities, with out calling is there any common place where script will be fired for all the entities 
    kumar
    Thursday, July 15, 2010 9:16 AM
  • "i am looking not to call on all entities, with out calling is there any common place where script will be fired for all the entities "

    if you are looking something globle JS, then there is not any supported way of doing this, only you have to change globle.js file in crm but that is not supported and also not recommended


    Mahain
    Thursday, July 15, 2010 9:21 AM
    Moderator
  • the only option you would have if you did not want to decalre/define the script to be included as the guys above have shown would be to place your code in an MSCRM script file like global.js (which I do strongly not reccommend as it will be unsupported and overwritten each time you apply a rollup.) the only solution is to include it during the onload of the required entities and this is also a much more efficient approach. Break your js files down into categories so that they include only the functionality required for an entity or set of common functions.
    MSCRM Bing'd - http://bingsoft.wordpress.com
    Thursday, July 15, 2010 9:23 AM
    Moderator
  • This little bit of code I found somewhere on the Interne t helps my Javascript Grid Editor load CRM scripts, the same should work for your code.  Be aware, however, that it works asynchronously:

    function IncludeExternalScript(targetDoc, filename) {
     var htmlDoc = targetDoc.getElementsByTagName("head").item(0);
     var js = targetDoc.createElement("script");
     js.setAttribute("type", "text/javascript");
     js.setAttribute("src", filename);
     htmlDoc.appendChild(js);
     
     return js;
    }

    However, I wonder if it doesn't also have the caching problem mentioned by Henry in his solution .  Indeed, the code above, and his first example are strikingly similar.  Another post here suggests that the only synchronous method of loading the scripts (which is usually preferable, since the calling script is likely to depend on the dynamically included one) is to create the "script" DOM elements with the scripts already inside them.  Well, ____, I use a variation of the function above for loading a portion of the AdvFind.aspx page (for the global variables that the field scripts require):

    function IncludeScript(targetDoc, script) {
     var htmlDoc = targetDoc.getElementsByTagName("head").item(0);
     var js = targetDoc.createElement("script");
     js.setAttribute("type", "text/javascript");
     js.text = script;
     htmlDoc.appendChild(js);
     
     return js;
    }
    

    So, if I were to rewrite the first function to utilize the second one (which I will promptly do for my JGE project, to hopefully resolve some issues with execution speed the first time its loaded):

    function IncludeExternalScript(targetDoc, filename) {
     var httpObj = new ActiveXObject("Msxml2.XMLHTTP");
     httpObj.open("GET", filename, false);
     httpObj.send(null);
    
     if (httpObj.status == 200 || httpObj.status == 304) {
      IncludeScript(targetDoc, httpObj.responseText);
     } else {
      alert("Failure loading external script: " + filename); 
     }
    }
    So, I have avoided using eval(), and possibly loaded an external script synchronously into the document, and merged the script seamlessly into the local namespace!  Anyone want to double-check my homework?


    Dave Berry - MVP Dynamics CRM - http:\\crmentropy.blogspot.com
    Thursday, July 15, 2010 6:17 PM
    Moderator
  • I found another blog that really helps understand how to dynamically load CSS and Javascript into DOM elements .  So far, my testing shows good performance, and better behavior than the way I had been doing these things.

    Dave Berry - MVP Dynamics CRM - http:\\crmentropy.blogspot.com
    Thursday, July 15, 2010 9:34 PM
    Moderator
  • To avoid modifying the DOM as the other examples show, as this is an unsupported approach to including scripts, you should use the following

    IncludeExternalScript = function(scriptFile) {
      try{
        var netRequest = new ActiveXObject("Msxml2.XMLHTTP");
        netRequest.open("GET", scriptFile, false);
        netRequest.send(null);
        eval(netRequest.responseText);
      }catch(ex)
      {
        alert('An error occurred setting up external scripts');
        return false;
      }

    return true;
    }

    if you do not want the script file to be cached then you can use  scriptFile + '?' + Math.random() as shown below instead.

    IncludeExternalScript = function(scriptFile) {
      try{
        var netRequest = new ActiveXObject("Msxml2.XMLHTTP");
        netRequest.open("GET", scriptFile + '?' + Math.random(), false);
        netRequest.send(null);
        eval(netRequest.responseText);
      }catch(ex)
      {
        alert('An error occurred setting up external scripts');
        return false;
      }

    return true;
    }

    This version of including scripts also makes the included script immediately available (eval) so you can just start calling your functions.


    MSCRM Bing'd - http://bingsoft.wordpress.com
    Friday, July 16, 2010 9:24 AM
    Moderator