admin管理员组文章数量:1328674
I want to use javascript library in java source code. I read sth about it and I read, that I should use ScriptInjector
. This class has 2 inner classes: ScriptInjector.FromString
and ScriptInjector.FromUrl
. I want to load javascript library from local file, so I should use from string. How to do it?
ScriptInjector.fromString("myLibrary.js");
does not work. Where to add library?
I want to use javascript library in java source code. I read sth about it and I read, that I should use ScriptInjector
. This class has 2 inner classes: ScriptInjector.FromString
and ScriptInjector.FromUrl
. I want to load javascript library from local file, so I should use from string. How to do it?
ScriptInjector.fromString("myLibrary.js");
does not work. Where to add library?
Share Improve this question edited Jul 8, 2014 at 8:27 Anders R. Bystrup 16.1k11 gold badges63 silver badges58 bronze badges asked Jul 7, 2014 at 12:33 user3240883user3240883 3094 silver badges11 bronze badges 1- I tried this: simon.pamies.de/archives/194 , but this is not working. – user3240883 Commented Jul 7, 2014 at 12:37
2 Answers
Reset to default 61) fromUrl
- creates script
tag with specified src attribute and appends it to the page. E.g.
ScriptInjector.fromUrl("http://example./my-script.js").inject();
will simply produce:
<script type="text/javascript" src="http://example./my-script.js" />
You can host your files on the web site and inject each of them on demand
2) fromString
- creates script
tag with specified body of the script, so:
ScriptInjector.fromString("alert('Injected!')").inject();
will give
<script type="text/javascript">
alert('Injected!')
</script>
In this case JS code is a part of your piled GWT code and browser doesn't require to load it with separate request. I think it is possible to include native JS file into piled output with TextResource
. So you need following:
Define resources
public interface JsResources extends ClientBundle {
final JsResources INSTANCE = GWT.create(JsResources.class);
@Source("first.js")
TextResource firstScript();
@Source("second.js")
TextResource secondScript();
}
Inject required script
ScriptInjector.fromString(JsResources.INSTANCE.firstScript().getText()).inject();
To use .fromString()
you'd have to load the JS into a String and pass that.
If you need to load the script using the .fromUrl()
you'll have to put it somewhere "Internet" accessible, since the inject()
ends up in
private static native void nativeSetSrc(JavaScriptObject element, String url) /*-{
element.src = url;
}-*/;
(See it here)
So: Extract or otherwise expose the script to your webserver.
Cheers,
本文标签: javaUsage of ScriptInjector in GWTStack Overflow
版权声明:本文标题:java - Usage of ScriptInjector in GWT - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742219738a2435229.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论