Alert before unload in Dart M8

2019-08-12 23:28发布

问题:

As of dart milestone 8, it is not possible to alert the user before (s)he navigates away from the page by the following method:

window.onBeforeUnload.listen((BeforeUnloadEvent event) {
    event.returnValue = 'Are you sure you want to leave?';
});

since the Event.returnValue field has been removed. How do you accomplish this effect with the new API?

This is how it's done with jQuery:

$(window).on('beforeunload', function(){
  return 'Are you sure you want to leave?';
});

回答1:

It looks like the API got dropped when we integrated a new Blink version- Blink finally added the BeforeUnloadEvent natively. Prior to that we had to fake it.

See bug https://code.google.com/p/dart/issues/detail?id=14641

Workaround: use it just as before. The event being passed in is a subclass of BeforeUnloadEvent and still has returnValue.



标签: jquery dart