Registering a jscript event handler

This forum is for questions / discussions regarding development of addons / tweaks for MediaMonkey.

Moderator: Gurus

refrancoeur
Posts: 13
Joined: Wed Oct 01, 2008 6:50 pm
Location: Washington, DC

Registering a jscript event handler

Post by refrancoeur » Fri Jan 30, 2009 8:17 pm

I am using a script to build an html window to display in a large font the characteristics of the track that is currently playing. So far it works like a charm, but now I want to add a button that will hide or show the window. I am able to register an onclick event with the button element in webbrowser activex control, but when I click on the button I get the "Error executing script event." If I include an onclick event in the html, the click has no effect. But, get this, when I do both, the javascript on click handler is invoked normally, but then the registered event handler is called and results in the same "Error executing script event."

I have not been able to find a single example of registering a jscript handler for an event, and I suspect that therein lies the problem. I have a similar problem associating hot keys with jscript scripts: I can make the association, but when I press the hot key the system assumes the script being invoked is in vb and then fails with a vb error message. (BTW, the script works perfectly when invoked from the tools menu.) I have a very important reason for using jscript, and I have written many scripts that work flawlessly. It seems that jscript works consistently within the MS scripting environment, but MM sometimes gets confused as to which is which. Has anyone else experienced such behavior, and has anyone suggestions as to how I can get around it?

TIA,
Dick Francoeur

Peke
Posts: 12472
Joined: Tue Jun 10, 2003 7:21 pm
Location: Serbia
Contact:

Re: Registering a jscript event handler

Post by Peke » Sat Jan 31, 2009 4:26 am

Have you tried to create New ActiveX Within MediaMonkey? Can you give us Script example so that we can look at it and see what has gone wrong?
Best regards,
Pavle
MediaMonkey Team lead QA/Tech Support guru
Admin of Free MediaMonkey addon Site HappyMonkeying
Image
Image
How to add SCREENSHOTS to forum

raybeau528
Posts: 401
Joined: Thu Sep 27, 2007 4:19 pm
Location: Connecticut

Re: Registering a jscript event handler

Post by raybeau528 » Sat Jan 31, 2009 9:10 am

I usually get that error with vbscript if I forget to include the 'arg' in the event sub.

Sub event_OnClick(arg)

End Sub

refrancoeur
Posts: 13
Joined: Wed Oct 01, 2008 6:50 pm
Location: Washington, DC

Re: Registering a jscript event handler

Post by refrancoeur » Sat Jan 31, 2009 2:20 pm

I have extracted the minimum code necessary to demonstrate what seems to be happening. Here it is:

Code: Select all

function HTMLWindow(html){
	with(this){
		this.form = SDB.UI.newForm;
		with(form){
			formPosition = (ScreenCenter=4);
			borderStyle = (DialogBorder=3);
			caption = "Tester";
			with(common){
				setRect(0,0,400,200);
				visible = false;
				stayOnTop = false;
			}
		}
		this.webBrowser = SDB.UI.newActiveX(form,"Shell.Explorer");
		with(webBrowser){
			setHTMLDocument(html);
			this.document = interf.document;
			with(common){
				visible = true;
				setRect(0,0,400,200);
				anchors = 1 + 2 + 4 + 8;
			}
		}
	}
	
	this.showModal = function(){
		this.form.showModal();
	}
	
	this.show = function(){
		with(this.form.common){
			visible = true;
			stayOnTop = true;
		}
	}
}

function modalMsg(msg){
	new HTMLWindow("<html><body>"+msg+"</body></html>").showModal();
}

function clicker(event){
	modalMsg("External");
}


function tester(){

/*
//(1)The following works:

	var window = SDB.Objects("Tester") = new HTMLWindow(
		"<html><body><button id=button onclick='alert(\"Internal\");'>Test</button></body></html>"
	);

	var button = window.document.getElementById("button");

	window.show();
//========================================================
*/
/*
//(2)The following does NOT work (nothing happens):

	var window = SDB.Objects("Tester") = new HTMLWindow(
		"<html><body><button id=button>Test</button></body></html>"
	);

	var button = window.document.getElementById("button");

	button.onclick = clicker;
	
	window.show();
//========================================================
*/
/*
//(3)The following does NOT work ("Error executing script event"):

	var window = SDB.Objects("Tester") = new HTMLWindow(
		"<html><body><button id=button>Test</button></body></html>"
	);

	var button = window.document.getElementById("button");

	Script.registerEvent(button,"onclick",clicker);
	
	window.show();
//========================================================
*/
///*
//(4)The following works partially:
//button.onclick works, but registered event fails with "Error executing script event"

	var window = SDB.Objects("Tester") = new HTMLWindow(
		"<html><body><button id=button>Test</button></body></html>"
	);

	var button = window.document.getElementById("button");

	button.onclick = clicker;//WORKS!!

	Script.registerEvent(button,"onclick",clicker);//FAILS!!
	
	window.show();
//========================================================
//*/
}
I am particularly interested in getting versions 2 or 3 to work. Since version 4 incorporates both versions 2 and 3, and the version 2 portion seems to work, it appears that version 2 should be able to work on its own.

Regards,
Dick Francoeur

raybeau528
Posts: 401
Joined: Thu Sep 27, 2007 4:19 pm
Location: Connecticut

Re: Registering a jscript event handler

Post by raybeau528 » Sat Jan 31, 2009 2:30 pm

ok, I don't know jscript, but does this evaluate to a boolean test rather than assignment?

Code: Select all

   var window = SDB.Objects("Tester") = new HTMLWindow
a = (b = c) : a is true/false depending on b and c.

Ray

refrancoeur
Posts: 13
Joined: Wed Oct 01, 2008 6:50 pm
Location: Washington, DC

Re: Registering a jscript event handler

Post by refrancoeur » Sat Jan 31, 2009 4:53 pm

Ray,

This is javascript not vbscript. The assignment operation = is strictly that -- assignment. The logical equality test is == as in java and c++. VB is riddled with such ambiguities (viz. the symbol ":"). Javascript also allows multiple assignments in one statement: a=b=c...=z results in everything equaling z. In vb this statement would be difficult at best to parse.

Regards,
Dick

raybeau528
Posts: 401
Joined: Thu Sep 27, 2007 4:19 pm
Location: Connecticut

Re: Registering a jscript event handler

Post by raybeau528 » Sat Jan 31, 2009 5:40 pm

Thanks for the explanation.

Ray

refrancoeur
Posts: 13
Joined: Wed Oct 01, 2008 6:50 pm
Location: Washington, DC

Re: Registering a jscript event handler

Post by refrancoeur » Mon Feb 02, 2009 2:42 pm

By experimentation, I have found the following:
  • It is possible to register an event for a DOM element by assigning it a js function as an "on" event (e.g. element.onclick=function(){...}).
    The event however is ineffective unless the Script.registerEvent is also called for any event on the element (e.g. Script.registerEvent(element,"oncut",function(event){});) The events need not (indeed should not [see below]) be the same. Apparently the webserver object needs to know that at least one event has been registered for the element, and the registerEvent accomplishes this.
    Unfortunately, the registerEvent does not seem to work properly for jscript handlers and results in an "error executing event .."
    Since the events need not be the same, I have temporarily circumvented the problem by calling the registerEvent for an event that is unused (at least for now), and the element.onclick handler does indeed get invoked normally.
The above is clearly not an acceptable long-term solution. The registerEvent method of the Script object should be modified to interface properly with jscript functions. I suspect that the same interfacing problem occurs for hot keys assigned to jscript scripts which also fail with vbscript error messages.

I would like to request that the registerEvent method of the Script object be so modified.

Best regards,
Dick Francoeur

jiri
Posts: 5392
Joined: Tue Aug 14, 2001 7:00 pm
Location: Czech Republic
Contact:

Re: Registering a jscript event handler

Post by jiri » Tue Feb 03, 2009 11:20 am

The problem in your code is that you pass function as a reference, instead of just its name, i.e. the correct code is:

Script.registerEvent(button,"onclick","clicker");

It works fine then (just tested).

Jiri

refrancoeur
Posts: 13
Joined: Wed Oct 01, 2008 6:50 pm
Location: Washington, DC

Re: Registering a jscript event handler

Post by refrancoeur » Tue Feb 03, 2009 1:41 pm

Jiri,

You're a wiz -- that's exactly the problem. It does however create another issue in jscript. The script I'm building requires that the event handlers be anonymous, which is perfectly valid in ECMA script (jscript, javascript). Using registerEvent apparently requires a named script as a handler. In coding terms, the following is needed:

Code: Select all

var button = window.document.getElementById("button");
button["onclick"] = function(event){...}; 
All of this is valid and useful jscript. Since the handler has no name, however, it cannot be referred to in the call to registerEvent, and if it is not, it will not be invoked.

On the other hand, your solution has given me a simple and valid approach to getting around this. Since the assigned and the registered handlers do not have to be the same, I define an empty function with a name and use it in the registerEvent call as follows:

Code: Select all

function _NOP_(event){}
...
var button = window.document.getElementById("button");
button["onclick"] = function(event){...}; 
Script.registerEvent(button,"onclick","_NOP_");
Works like a charm.

Though this gets me off the hook for now, it is probably not the best long term solution since it does add some overhead. A better approach might be to allow a call to registerEvent with perhaps a null handler whose only purpose would be to register the element as having other handlers.

Thanks a million for helping me out with this -- you gave me exactly what I needed.

Best regards,
Dick

Post Reply