How to call external function defined in microsoft jscript

Hi there,
I need helps.

I have a function defined in hi.js.
-------------------------
function hi(){
 WScript.echo("hello !")
}
-------------------------

Now, I want to call this function in callhi.js

-------------------------
hi();
-------------------------

I put both of them in a directory. But it does not work.
I don't know why, because in WinXP, C:\WINDOWS\PCHealth\HelpCtr\System\scripts, they can call external function. See Common.js and HomePage__SERVER.js in this directory.

Regrads,
Tany.




Answer this question

How to call external function defined in microsoft jscript

  • Peter Schmitz

    If there is no mechanism in the host you are using, the only other possible solution, though a bit of a hack, is to read the second file into a string and then eval it. This may not work depending on the security settings if the host locks down the engine to prevent execution of unsafe code.

    e.g.
    var fso = new ActiveXObject("Scripting.FileSystemObject");
    var HomePage_Server_Code = fso.OpenTextFile("HomePage__Server.js").ReadAll();
    eval(HomePage_Server_Code);


  • Prince Chalakkal

    The host that runs the scripts will determine what other scripts are available.
    For WSH, you can use a WSF file to import the script. e.g. Save to callhi.wsf and run.
    <job>
    <script language="jscript" src="hi.js"/>
    <script language="jscript">
    hi();
    </script>
    </job>


  • irg

    Thank Rok Yu,
    I knew this solution with WSH.
    My question is how they can do without using WSH.

    See the common.js in which the function Common_GetTopPosition( elem, fStopOnAbsolute ) is defined.


    //
    // Copyright (c) 2000 Microsoft Corporation
    //

    //////////////////////////////////////////////////////////////////////

    var MF_SEPARATOR = 0x00000800;

    var MF_ENABLED   = 0x00000000;
    var MF_GRAYED    = 0x00000001;
    var MF_DISABLED  = 0x00000002;

    var MF_UNCHECKED = 0x00000000;
    var MF_CHECKED   = 0x00000008;

    var MF_UNHILITE  = 0x00000000;
    var MF_HILITE    = 0x00000080;

    //////////////////////////////////////////////////////////////////////

    ...

    //////////////////////////////////////////////////////////////////////

    function Common_GetTopPosition( elem, fStopOnAbsolute )
    {
       var elem;
       var lTopPixel = 0;

       while(elem != null)
       {
        var fIsAbsolute = (elem.style.position == "absolute");

        if(fIsAbsolute && fStopOnAbsolute) break;

           lTopPixel += elem.offsetTop;

        elem = elem.offsetParent;

        if(fIsAbsolute)
        {
         while(elem != null && elem.style.position != "absolute") elem = elem.offsetParent;
        }
       }

       return lTopPixel;
    }

    ...

    And this function is called in HomePage__SERVER.js

    //
    // Copyright (c) 2001 Microsoft Corporation
    //

    ...

    function node_Server_over()
    {
        var node = node_Server_find( event.srcElement ); if(!node) return;

        {
            var root = node.HC_ROOT;
            var top  = node;
            var sel  = [];
            var i;

            if(root.HC_SELECTED == node)
            {
                root.HC_PENDING  = false;
                return;
            }

            while(top.HC_PARENT)
            {
                sel[top.uniqueID] = 1;

                top = top.HC_PARENT;
            }

            for(i in root.HC_CHAIN)
            {
                if(!selIdea)
                {
                    node_Server_out_final( root.HC_COLLIdea );
                }
            }

            root.HC_CHAIN    = sel;
            root.HC_SELECTED = node;
            root.HC_PENDING  = false;
        }

        if(node.HC_FLYOUT && !node.HC_SUBNODES)
        {
            Server_Build( node );
        }

        switch(node.HC_POSITION)
        {
        case "root"  : node.className = "Taxo-Block sys-background-strong sys-toppane-color-border Taxo-Block-Highlight"       ; break;
        case "single": node.className = "Taxo-Block sys-background-strong sys-toppane-color-border Taxo-Block-Highlight-Single"; break;
        case "first" : node.className = "Taxo-Block sys-background-strong sys-toppane-color-border Taxo-Block-Highlight-First" ; break;
        case "middle": node.className = "Taxo-Block sys-background-strong sys-toppane-color-border Taxo-Block-Highlight-Middle"; break;
        case "last"  : node.className = "Taxo-Block sys-background-strong sys-toppane-color-border Taxo-Block-Highlight-Last"  ; break;
        }

        var cell = node.HC_ATTACHFLYOUT;
        if(cell && cell.style.display == "none")
        {
            cell.style.display = "";

            var div  = cell.firstChild;
      var left = Common_GetLeftPosition( cell, true ) + cell.offsetWidth - 1;
      var top  = Common_GetTopPosition ( cell, true ) - 1;
      var room;

      room = document.body.clientHeight - (top - document.body.scrollTop);

      if(room < div.offsetHeight)
      {
          top += room - div.offsetHeight; if(top < 0) top = 0;
      }

      if(window.document.documentElement.dir == "rtl")
      {
       div.style.right = document.body.clientWidth - left;
      }
      else
      {
             div.style.left = left;
      }

            div.style.top  = top;
        }
    }


    Regards,
    Tany.



  • KaliBaba

    <Tany Pham@discussions.microsoft.com> wrote in message
    news:b91a5f23-bad5-4388-be42-454c5e5a7d6c@discussions.microsoft.com
    > I knew this solution with WSH.
    > My question is how they can do without using WSH.
    >
    > See the common.js in which the function Common_GetTopPosition( elem,
    > fStopOnAbsolute ) is defined.
    >
    > And this function is called in HomePage__SERVER.js
    And how is HomePage__SERVER.js being run What's hosting it As Rok Yu says, there is no way from inside JavaScript to "import" an external .js file, the solution is necessarily host-specific.
    --
    With best wishes,
        Igor Tandetnik

  • How to call external function defined in microsoft jscript