How to load both html and javascript into webengine from loadContent()?


Could someone provide some suggestions on how to load the following onto webviewer from loadContent()?

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");
11/23/2013 3:21:25 PM

I just found out that using the <base> tag in the HTML also does the trick:

        <title>The slash at the end of the href is important!</title>
        <base href="file:///absolute/path/to/your/docroot/" />
        <img src="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.

This has been tested with WebView of Java 8u25.

10/21/2014 1:56:09 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow