如何将 html 和 javascript 从 loadContent() 加载到 webengine 中? [英] How to load both html and javascript into webengine from loadContent()?
问题描述
有人可以提供一些关于如何从 loadContent() 将以下内容加载到 webviewer 的建议吗?
Could someone provide some suggestions on how to load the following onto webviewer from loadContent()?
http://jsbin.com/aqupun/6/edit
我试图做这样的事情,但它似乎不起作用.谢谢!
I was trying to do something like this, but it doesn't seem to work. Thanks!
Scanner sc1 = new Scanner(new File("src/web/web.html"));
String webStr = sc1.useDelimiter("\Z").next();
Scanner sc2 = new Scanner(new File("src/web/data.js"));
String dataStr = sc2.useDelimiter("\Z").next();
Scanner sc3 = new Scanner(new File("src/web/cytoscape.min.js"));
String cytoStr = sc3.useDelimiter("\Z").next();
Scanner sc4 = new Scanner(new File("src/web/jquery.min.js"));
String jqueryStr = sc4.useDelimiter("\Z").next();
webEngine.loadContent(cytoStr, "text/javascript");
webEngine.loadContent(jqueryStr, "text/javascript");
webEngine.loadContent(dataStr, "text/javascript");
webEngine.loadContent(webStr, "text/html");
推荐答案
我刚刚发现在 HTML 中使用
标签也能解决问题:
I just found out that using the <base>
tag in the HTML also does the trick:
<html>
<head>
<title>The slash at the end of the href is important!</title>
<base href="file:///absolute/path/to/your/docroot/" />
</head>
<body>
<img src="image.png"/>
</body>
</html>
如果你通过 engine.loadContent(String)
加载上面的代码,那么 image.png
将从 /absolute/path/to/your/加载docroot/image.png
.
If you load the above code via engine.loadContent(String)
then image.png
will be loaded from /absolute/path/to/your/docroot/image.png
.
如果您需要加载多个资源,这种方法更容易,因为您只需在一个地方指定绝对路径.
This method is easier if you need to load multiple resources since you only have to specify the absolute path at a single place.
这已经用 Java 8u25 的 WebView
测试过.
This has been tested with WebView
of Java 8u25.
这篇关于如何将 html 和 javascript 从 loadContent() 加载到 webengine 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!