How can my javascript tell if it was called by WScript or the browser?

I am trying to write a javascript program that can be called from WScript or browser (embedded in html). Many javascript functions are independent of the caller type, but not debug functions such as "window.write" or "WScript.alert".

I know javascript functions can figure out the name of their caller, but not the main javascript programs.

Case 1: The caller is WScript, WScript sample.js

Case 2: the calling browser,

How can sample.js determine if it was called by WScript or by the browser?

+1


source to share


1 answer


You can check if your script was called from WScript or browser by checking for the presence / absence of WScript / window objects. The browser does not have a built-in WScript object, and a WScript script usually does not have access to the window object (unless you created one).

For example...



function Test()
{
    if(typeof WScript!= "undefined")
    {
        WScript.Echo("Hello WScript!");
    }
    else if (typeof window != "undefined")
    {
        alert("Hello browser!");
    }
}

      

+3


source







All Articles