Create JavaScript custom event -
I would like to create a custom event in javascript.
I have a WPF application with a webbrose inside, and an HTML page with javascript.
I work with a printer. When the printer situation changes, it triggers an event in the NAT.
Then, I call a JavaScript method OnPrinterStateChanged (state)
with InvokeScript
WebBrowser control function.
The problem is that I have to implement OnPrinterStateChanged (state)
in my webpage. I can not change the name of this method or cancel the membership of the event. I am ...
I want to move the JavaScript method to OnPrinterStateChanged (state)
in a different JavaScript file.
What do I want:
- Subscribe / unsubscribe to events in my HTML page and decide what to do when I trigger the event (eg. : "ChangeState function")
- When the .net event is triggered, it calls the
OnPrinterStateChanged (state)
of my separate .js file, then the Javascript event is triggered. And the function is calledChangeState
.
I have found some solutions but I did not manage to work it ... What is the simplest way to do this?
Maybe something like that?
Function OnPrinterStateChanged (state) {var evt = new CustomEvent ('PrinterStateWorld', {Extension: State}); Window.dispatchEvent (evt); } // Listen to your custom event window. AddEventListener ('printstatechanged', function (e) {console.log ('printer state changed', e.detail);});
An alternative solution would be to use the function structure, but then it would be difficult to remove specific listeners.
Type the Function OnPrinterStateChanged (state) {} function (fn1, fn2) {return function () {fn1.apply (this, logic); Fn2.apply (this, argument); }; } // Add a new listener; OnPrinterStateChanged = compose (OnPrinterStateChanged, function (state) {console.log ('listener 1');}); // Add another OnPrinterStateChanged = compose (OnPrinterStateChanged, Function (state) {console.log ('Listener 2');});
Edit:
Here you can do this with jQuery.
function OnPrinterStateChanged (state) {var evt = $ event ('printerstatechanged'); Evt.state = State; $ (Window) .trigger (evt); } // Listen to your custom event $ (window) .on ('printerstatechanged', function (e) {console.log ('Printer Status Changed', E.State);});
Comments
Post a Comment