javascript - GWT:将处理程序添加到本机 js 事件 onafterprint
问题描述
我有一个代表文档打印预览的 GWT 小部件。在此小部件中,打印按钮/操作由外部 Chrome 服务管理。我需要从我的 java 代码中检测本机 window.onafterprint 事件。
我怎样才能做到这一点?
提供代码没有成功:
var popup = $wnd.open(url, title, ""); // this open the preview window
popup.onafterprint = function (ev) {
$wnd.console.log("mess 2"); // to try to see if detect the print button
}
解决方案
实现此目的的一种方法是将 Java 处理程序导出到 Javascript,这样 Javascript 事件可以调用 Java 处理程序:
public MyUtilityClass {
public static int handleOnAfterPrint(String message) { ... }
public static native void exportStaticMethod() /*-{
$wnd.handleOnAfterPrint =
$entry(@mypackage.MyUtilityClass::handleOnAfterPrint(Ljava/lang/String;));
}-*/;
}
所以你可以这样做:
popup.onafterprint = function (ev) {
handleOnAfterPrint("mess 2");
}
推荐阅读
- c# - 如果可能,缩短字符串并分配给变量
- reactjs - 反应钩子中的最大更新深度超出错误
- python - Pandas 一次交换多个索引级别
- javafx - javafx : 在 javafx 中扩展一个类场景
- git - 使用 husky 解析 git commit 消息
- esp8266 - esp8266 不与指纹传感器 R305 通信
- python - 在 Pyspark 中不使用 UDF 访问 Spark DataFrame 中的向量元素
- php - URL重写和Wordpress?
- python - 以元组作为参数解包 UDF 中的元组
- javascript - FileReader:reader.result 显然不是 null