我有一个GWT项目,我想在GWT项目的主html文件中添加一个脚本标记,该文件调用位于我的客户端代码中的Java函数.
根据文档,我应该添加类似下面的html标记:
<script type='text/javascript'>
this.@com.myCompany.myProject.client.myClass::myFunction();
</script>
Run Code Online (Sandbox Code Playgroud)
其中com.myCompany.myProject.client.myClass是类路径,myFunction是我想调用的java函数.
当我尝试使用myFunction的以下实现时,没有任何反应:
public void myFunction() {
HTMLPanel panel = new HTMLPanel("I have been called");
RootPanel.get().add(panel);
}
Run Code Online (Sandbox Code Playgroud)
也就是说,myFunction没有被调用.
但是当我从JSNI方法进行相同的调用时,它就可以工作了.
是否可能无法通过html脚本进行调用,或者我做错了什么?
谢谢!
我有两个js文件,每个文件都有自己的window.onload处理程序。根据我将两个onload处理程序附加到窗口对象的方式,我在第二个处理程序上得到了不同的行为。
更具体地说,这是我的html文件:
<html>
<head>
<title>Welcome to our site</title>
<script type="text/javascript" src="script1.js"> </script>
<script type="text/javascript" src="script2.js"> </script>
</head>
<body id="pageBody">
<h2 align="center">
<a href="http://www.whatever.com" id="redirect"> Wellcome to our site... c'mon in! </a>
</h2>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
它加载两个js文件,script1.js和script2.js。
这是这两个脚本的版本,它们导致(至少在我看来)意外的行为。
Script1.js:
window.onload = initAll1(); // attach first onload handler
function initAll1() {
alert("initAll1");
document.getElementById("redirect").onclick = foo; // attach an onclick handler
}
function foo() {
alert("we are in foo");
return false;
}
Run Code Online (Sandbox Code Playgroud)
Script2.js:
addOnloadHandler(initAll2); // with this we should attach a second onload handler …Run Code Online (Sandbox Code Playgroud) 我有一个chrome扩展,它使用内容脚本来动态插入引用外部javascript文件的脚本标记.我使用的代码是:
var html_doc = document.getElementsByTagName('head')[0];
var _js = document.createElement('script');
_js.setAttribute('type', 'text/javascript');
_js.setAttribute('id', 'chr_js');
_js.setAttribute('src', 'http://unixpapa.com/js/dyna1.js');
if(!document.getElementById('chr_js'))
html_doc.appendChild(_js);
Run Code Online (Sandbox Code Playgroud)
外部Javascript包含以下代码:
function lfunc(){
alert('RUNNING loaded function');
}
alert('LAST LINE of script');
Run Code Online (Sandbox Code Playgroud)
当我在选项卡中加载页面时,会出现" 最后一行脚本 "消息,显示脚本标记已正确插入DOM中.
我的扩展程序还有一个按钮(即browser_action).现在,我希望这个按钮在单击时调用上面定义的lfunc().不幸的是,我的代码根本不起作用.
我在background.html页面中使用以下代码来处理我的按钮的onClick事件:
<script>
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.tabs.executeScript(null,
{code: "try {lfunc()} catch (e) {alert(e);}"});
}); // it should call lfunc()
</script>
Run Code Online (Sandbox Code Playgroud)
manifest.json文件中的权限是:
"permissions": [
"tabs", "http://*/*", "https://*/*" ]
Run Code Online (Sandbox Code Playgroud)
我没有收到" RUNNING loaded function "消息,而是收到错误消息" ReferenceError:lfunc not defined ".
我究竟做错了什么?