admin管理员组

文章数量:1391976

I am trying to trigger events (backbutton, pause, resume etc) in cordova manually from the browser (for debugging). I am doing it in the following way:

$(window).trigger('backbutton');

When we register the event handler using $(document).on('backbutton', handlerCode);

The backbutton event gets triggered, however when we register it using the syntax in the cordova documentation:

document.addEventListener("backbutton", onBackKeyDown, false);

the event handler is not triggered. I noticed this when I used $(window).trigger('backbutton'), the code did not hit the cordova.js file. What would be the right way to invoke events in cordova application from the browser?

I am trying to trigger events (backbutton, pause, resume etc) in cordova manually from the browser (for debugging). I am doing it in the following way:

$(window).trigger('backbutton');

When we register the event handler using $(document).on('backbutton', handlerCode);

The backbutton event gets triggered, however when we register it using the syntax in the cordova documentation:

document.addEventListener("backbutton", onBackKeyDown, false);

the event handler is not triggered. I noticed this when I used $(window).trigger('backbutton'), the code did not hit the cordova.js file. What would be the right way to invoke events in cordova application from the browser?

Share Improve this question edited Jun 23, 2020 at 19:16 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Sep 5, 2014 at 12:35 HarshitHarshit 1232 silver badges11 bronze badges 4
  • 1 Try cordova.fireDocumentEvent('backbutton') and see if that works better? – Kerri Shotts Commented Sep 5, 2014 at 21:19
  • Tried cordova.fireDocumentEvent('backbutton') also, but the control did not return to the event handler for the backbutton – Harshit Commented Sep 6, 2014 at 11:35
  • You said "What would be the right way to invoke events in cordova application from the browser?". You can't test those events on the browser, you need a phone – Emre Commented Sep 6, 2014 at 20:54
  • Yes, but my requirement is to simulate firing events in browser during development, before creating the app and deploying to the phone. All I need is a way to fire events so that after firing events, the control returns to the event handler. – Harshit Commented Sep 7, 2014 at 3:58
Add a ment  | 

1 Answer 1

Reset to default 6

It is possible to fire the events from browser. In order to fire the events, it is required to either fireWindowEvent or fireDocumentEvent.

To fire the pause, resume, online, offline, backbutton, menubutton, searchbutton, startcallbutton, endcallbutton, volumeupbutton, volumedownbutton, use the fireDocumentEvent. eg, firing 'pause' event:-

cordova.fireDocumentEvent('pause',{});

In order to fire events related to battery, use fireWindowEvent. eg, firing 'batterycritical' event:-

cordova.fireWindowEvent('batterycritical', {"level":"20","isPlugged":true});

本文标签: javascriptFiring events in Cordova applicationsStack Overflow