Language Engines - JScript

Does Microsoft have a FAQ document for JScript?

Yes. You can find it at http://msdn.microsoft.com/scripting/default.htm?/scripting/jscript/techinfo/JSfaq.htm

(Source: mark_baker@mindspring.com, Mark Baker, 12/8/99)

Back to Table of Contents


I'm trying to decide between JScript & VBScript. Which one should I go with?

Microsoft Program Manager Andrew Clinick wrote an excellent article for his "Scripting Clinic" column on MSDN addressing the pros/cons of each language. The article is too lengthy to include here, so go to MSDN (or Microsoft's site) and search for the article titled "Clinick's Clinic on Scripting #1: VBScript or JScript?

(Source: mark_baker@mindspring.com, Mark Baker, 11/8/1999)

Back to Table of Contents


Why can't I use the JScript function "alert()" in my script?

The alert function is not a part of the JScript language. It is a function exposed by the Web Browser object model into scripts running in the browser. You have to write your own message box mechanism.

(Source: Chris Sousa, 9/24/99, microsoft.public.scripting.hosting)

Back to Table of Contents


How can I get JScript to ignore event handlers for objects that don't yet exist in my script (because they are added at runtime), but activate them once the objects do exist?

You can hook up events by saying:


	foo.onsomeevent = myFunction;

	function myFunction()
	{
		// bla
	}

(Source: Peter Torr, Microsoft Windows Script Program Manager, 9/13/99, microsoft.public.scripting.hosting)

Back to Table of Contents


Can I use SAFEARRAY's with JScript?

JScript doesn't support SAFEARRAYs so you have to convert them.

(Source: Joe Graf, 11/16/99, microsoft.public.scripting.hosting)

Back to Table of Contents


How can I access JScript Array's within my script host that were created in a script?

To access JScript arrays from your script host, you should use the IActiveScript::GetScriptDispatch() method. Here are the basic steps to do so. Assume that the following code has been passed into IActiveScriptParse::ParseScriptText with the pstrItemName parameter set to NULL.


	var myArray = new Array(10);
	for (i = 0; i < 10; i++)
	{
		myArray[i] = i;
	}

Call IDispatch::Invoke on the array's IDispatch pointer with DISPID_NEWENUM to get an IUnknown pointer. Call IUnknown::QueryInterface for IID_IEnumVARIANT to get an IEnumVARIANT interface that can be used to enumerate the array. - or - Call IDispatch::Invoke on the array's IDispatch pointer with DISPID_GET_SAFEARRAY(-2700L) to get a SAFEARRAY containing the data from the array.

(Source: Joel Alley, Microsoft Developer Support, 12/1/99, microsoft.public.scripting.hosting)

Here is what posted to ATL list awhile ago: [ http://discuss.microsoft.com/SCRIPTS/WA-MSD.EXE?A2=ind9812C&L=ATL&P=R483 ]

Here is the code to convert JScript array to SafeArray vector. It depends on my public set of ATL extensions for auto pointers and error handling stuff: http://www.geocities.com/~andien/atlaux.htm Feel free to customize it for your own needs and environment.

(Source: Andrew Nosenko, 12/1/99, microsoft.public.scripting.hosting)

Back to Table of Contents


How can I write to a JScript Array from within my script host that was created in a script?

Looking back at our example JScript file, we could build on step #2 with the following code to change the fourth element of the array from 4 to 6.


	DISPID dispid;
	LPOLESTR value = L"4";
	HRESULT hr = S_OK;

	hr = pDispatch->GetIDsOfNames( IID_NULL, &value, 1, 0,&dispid);

	DISPPARAMS l_dispParams;
	EXCEPINFO l_exceptions;
	UINT l_error = 0;

	VARIANT* pArgument = new VARIANT;
	VariantInit( pArgument );
	pArgument->vt = VT_I4;
	pArgument->lVal = 6;

	l_dispParams.rgvarg = pArgument;
	l_dispParams.rgdispidNamedArgs = NULL;
	l_dispParams.cArgs = 1;
	l_dispParams.cNamedArgs = 0;

	/*Special stuff for property puts... */
	DISPID dispidNamed = DISPID_PROPERTYPUT;
	l_dispParams.rgdispidNamedArgs = &dispidNamed;
	l_dispParams.cNamedArgs = 1;

	hr = pDispatch->Invoke( dispid, IID_NULL, 0,DISPATCH_PROPERTYPUT, &l_dispParams,
										NULL, &l_exceptions, &l_error);

(Source: Eric Lippert, Microsoft Scripting Dev, Date Unknown, microsoft.public.scripting.hosting)

Back to Table of Contents


Why do I have to call IActiveScript::AddScriptlet to connect an object to JScript event handling code, but not for VBScript?

Unfortunately, JScript doesn't support "auto-magic" events like VBScript does. JScript doesn't recognized an event by it's naming convention, so the host has to do extra work to identify it. The way it does this is by using the AddScriptlet method.

(Source: Joel Alley, Microsoft Developer Support, 1/4/2000, microsoft.public.scripting.hosting)

Back to Table of Contents


Copyright © 1999-2001  Mark M. Baker