GWT - IE 不下载 UiBinder 中包含的外部脚本
我的 GWT 应用程序中有一些外部 Javascript 文件,我只想在用户访问需要它们的部分时下载这些文件。我将 JS 文件包含在 UiBinder 页面中,如下所示:
<g:HTMLPanel>
<script src="blah.js"/>
</g:HTMLPanel>
加载视图后不一定会下载文件,因此当我加载视图时,我会检查是否有异常表明外部代码不可访问,并且重试,直到可以无异常地创建视图。
这在 Firefox 中有效,但在 IE7/IE8 中无效。我使用 AJAX 版本来跟踪 IE 中发生的情况,并发出了对该文件的请求,但它没有下载任何内容,大小为 0。Web 服务器访问日志不显示该文件已被请求。
知道发生了什么事吗?
或者有更好的方法来做到这一点吗?到目前为止我所做的调查并不表明 GWT 有一种方便的方法来做到这一点。
更新:
虽然我们还没有使用 2.4,但为了尝试,我复制了 ScriptInjector 所需的源文件,并尝试按照 Thomas 的建议使用它。 JS 文件在两个浏览器中都能正确下载,但我收到“$wnd.xxxx 未定义”异常,表明无法找到外部脚本中的引用。我不会尝试加载我的小部件,直到 onSuccess() 调用从脚本注入返回 - 所以我的代码看起来像这样:
ScriptInjector.fromUrl("../xxxx.js").setCallback(
new Callback<Void, Exception>() {
public void onFailure(Exception reason) {
Window.alert("Script load failed.");
}
public void onSuccess(Void result) {
Window.alert("Script load success.");
MyWidget widget = new MyWidget();
client.onSuccess(widget); // adds the widget to the view
}
}).inject();
我也没有看到在 Firebug 中添加到 DOM 的 JS 文件引用,但我想知道是否这是因为它们被添加到页面 GWT 部分的 DOM,而不是实际的 index.jsp。
I have some external Javascript files in my GWT app that I only want to download when the user accesses the section that requires them. I included the JS files in a UiBinder page like so:
<g:HTMLPanel>
<script src="blah.js"/>
</g:HTMLPanel>
The files won't necessarily be downloaded as soon as the view is loaded, so when I load the view, I check for an exception indicating that the external code isn't accessible and try again until the view can be created without an exception.
This works in Firefox but it doesn't work in IE7/IE8. I used AJAX Edition to trace what's happening in IE, and a request for the file is made but it doesn't download anything, the size is 0. The web server access logs don't show that the file was requested.
Any idea what's going on?
Or is there a better way to do this? The investigating I've done thus far does not indicate that GWT has a convenient way to do this.
Update:
Although we're not on 2.4 yet, just to try I copied in the source files needed for ScriptInjector and attempted to use that per the suggestion by Thomas. The JS files download correctly in both browsers, but I get the '$wnd.xxxx is undefined' exception indicating that the references in the external script can't be found. I don't try to load my widget until the onSuccess() call returns from the script inject - so my code looks something like this:
ScriptInjector.fromUrl("../xxxx.js").setCallback(
new Callback<Void, Exception>() {
public void onFailure(Exception reason) {
Window.alert("Script load failed.");
}
public void onSuccess(Void result) {
Window.alert("Script load success.");
MyWidget widget = new MyWidget();
client.onSuccess(widget); // adds the widget to the view
}
}).inject();
I also don't see the JS file references added to the DOM in Firebug, but I wonder if that is because they are being added to the DOM of the GWT portion of the page, not to the actual index.jsp.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要告诉 ScriptInjector 将脚本添加到顶层窗口。
you need to tell the ScriptInjector to add the script to the top level window.
使用
innerHTML
(这是HTMLPanel
使用的)插入的脚本元素不会被执行。您必须使用
ScriptInjector
将脚本动态注入到您的页面中。Script elements inserted using
innerHTML
(which is whatHTMLPanel
uses) are not executed.You'll have to use
ScriptInjector
to dynamically inject a script into your page.