[英]GWT: Calling a javascript function from Java
嗨,我正在嘗試從Java調用.js文件中的函數。
在我的Java代碼中
@Override
public native void test()
/*-{
//JAVASCRIPT
$wnd.h8();
}-*/;
方法內部只是一個Alert起作用,但是我不能調用我的函數之一。 我已經嘗試過$ wnd,$ doc而已。
說我有一個functions.js,里面有這個:
function h8(){
alert("hi");
}
在我的索引中,我有:
<!doctype html>
<html>
<head>
<title>GWT Test</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<link href="styles.css" rel="stylesheet" type="text/css">
<script src="js/functions.js"></script>
</head>
<body>
<div align="center" id="embed-html"></div>
<script type="text/javascript" src="html/html.nocache.js"></script>
</body>
</html>
因此,您看到的是我正在html中加載腳本文件,但出現“未定義不是函數”錯誤。 Whithout $ doc或$ wnd我收到“找不到變量h8”錯誤。
這是控制台錯誤:
GwtApplication: exception: (TypeError)
line: 112978
column: 10
sourceURL: http://127.0.0.1:9876/html/52574FB8FF8725CA72DFF813B62FEE86.cache.js
__gwt$exception: <skipped>: undefined is not a function (evaluating '$wnd.h8()')
(TypeError)
line: 112978
column: 10
sourceURL: http://127.0.0.1:9876/html/52574FB8FF8725CA72DFF813B62FEE86.cache.js
__gwt$exception: <skipped>: undefined is not a function (evaluating '$wnd.h8()')
您應該定義Window函數:
Window.h8 = function() {
alert("!!!");
};
並使用$ wnd訪問它。
private static native void callFunction() /*-{
$wnd.h8();
}-*/;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.