locked
How to create global function? RRS feed

  • Question

  • Hi,

    Can i create global function in CRM like GenerateAuthenticationHeader(),?

    if this is possible, can function gets the parameters from the input varible in my on_load event? and I get a return value from function?

    thanks,

    Glo


    Tuesday, February 2, 2010 12:36 PM

Answers

  • You can define function in Global.js located in CRMWeb/static/common/scripts/

    As you modify system files it is unsupportes sollution.
    David Kolodziejczyk, team4crm | dawid.kolodziejczyk@team4crm.com | http://team4crm.com
    Wednesday, February 3, 2010 11:08 AM
  • Another option is to drop a js file into the ISV folder (we use ISV\_js\utils.js) and then in your onload event have the following lines:

    function LoadFile(url) {
    var httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
    httpRequest.open("GET", url+"?r="+Math.random(), false);  //The random is to prevent IE from caching, remove when done development
    httpRequest.send(null);
    return httpRequest.responseText;
    }
    var jsFile = LoadFile("/isv/_js/pareto_utils.js");
    eval(jsFile);

    The other methods is to write a <script> element to the <head> of the dom but I prefer this method solely because we try to avoid writing to the dom as much as we can to stay supported.

    Hope this helps
    Wednesday, February 3, 2010 4:59 PM
  • Load the script in the onload of a form like Pareto mentions except use the following method. Note that the onreadystatechange event is used to make sure that the file loads before you call its functions. I use many files throughtout a solution to keep the load time as low as possible.

    var script = document.createElement('script');
    script.language = 'javascript';
    script.src = '/ISV/Scripts/scriptFile.js'; // Place script files in ISV folder
    document.getElementsByTagName('head')[0].appendChild(script);

    var CallScriptFunctions = function()
    {
      if (event.srcElement.readyState == "loaded")
       thisIsMyFunction(); // function from scriptFile.js
    }

    script.attachEvent("onreadystatechange", CallScriptFunctions);

    Friday, February 5, 2010 1:01 AM
    Moderator

All replies

  • You can define function in Global.js located in CRMWeb/static/common/scripts/

    As you modify system files it is unsupportes sollution.
    David Kolodziejczyk, team4crm | dawid.kolodziejczyk@team4crm.com | http://team4crm.com
    Wednesday, February 3, 2010 11:08 AM
  • Another option is to drop a js file into the ISV folder (we use ISV\_js\utils.js) and then in your onload event have the following lines:

    function LoadFile(url) {
    var httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
    httpRequest.open("GET", url+"?r="+Math.random(), false);  //The random is to prevent IE from caching, remove when done development
    httpRequest.send(null);
    return httpRequest.responseText;
    }
    var jsFile = LoadFile("/isv/_js/pareto_utils.js");
    eval(jsFile);

    The other methods is to write a <script> element to the <head> of the dom but I prefer this method solely because we try to avoid writing to the dom as much as we can to stay supported.

    Hope this helps
    Wednesday, February 3, 2010 4:59 PM
  • Load the script in the onload of a form like Pareto mentions except use the following method. Note that the onreadystatechange event is used to make sure that the file loads before you call its functions. I use many files throughtout a solution to keep the load time as low as possible.

    var script = document.createElement('script');
    script.language = 'javascript';
    script.src = '/ISV/Scripts/scriptFile.js'; // Place script files in ISV folder
    document.getElementsByTagName('head')[0].appendChild(script);

    var CallScriptFunctions = function()
    {
      if (event.srcElement.readyState == "loaded")
       thisIsMyFunction(); // function from scriptFile.js
    }

    script.attachEvent("onreadystatechange", CallScriptFunctions);

    Friday, February 5, 2010 1:01 AM
    Moderator