views:

781

answers:

1

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

I am aware that javascript functions can determine the name of their caller, but not javascript main programs.

Case 1: caller is WScript, WScript sample.js

Case 2: caller is browser,

How can sample.js determine whether it was called by WScript or a browser?

+2  A: 

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

For example...

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