Skip to content

Instantly share code, notes, and snippets.

@mohamnag
Forked from bugabinga/WebEngineLoadBug.java
Last active March 9, 2016 13:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mohamnag/5e5b52a0800009f4c816 to your computer and use it in GitHub Desktop.
Save mohamnag/5e5b52a0800009f4c816 to your computer and use it in GitHub Desktop.
Attempt to reproduce a WebEngine loading bug in JavaFX (failed)
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Document</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-2.2.1.min.js"></script>
</head>
<body>
<h1 id="title">FILE 1</h1>
<a href="#" onclick="event.preventDefault(); ctr.load2();">load 2</a>
<script>
alert('file1');
$(document).ready(function () {
// use variableName here
$("#title").html(variableName);
alert(variableName);
});
</script>
</body>
</html>
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Document</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-2.2.1.min.js"></script>
</head>
<body>
<h1 id="title">FILE 2</h1>
<a href="#" onclick="event.preventDefault(); ctr.load1();">load 1</a>
<script>
alert('file2');
$(document).ready(function () {
// use variableName here
$("#title").html(variable2Name);
alert(variable2Name);
});
</script>
</body>
</html>
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebEvent;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
import netscape.javascript.JSObject;
/**
* https://stackoverflow.com/questions/35758313/consistent-setmember-on-javafx-window
*
* @author okr
* @date 08.03.2016
*/
public class WebEngineLoadBug extends Application {
private WebEngine webEngine;
private WebView webView;
/**
* @param args ignored.
*/
public static void main(final String[] args) {
launch(args);
}
@Override
public void start(final Stage primaryStage) throws Exception {
webView = new WebView();
webEngine = webView.getEngine();
// alert is the safest way to pass on data
webEngine.setOnAlert(new EventHandler<WebEvent<String>>() {
@Override
public void handle(WebEvent<String> event) {
System.out.println(event.getData());
}
});
primaryStage.setScene(new Scene(webView));
primaryStage.show();
load1();
}
public void load2() {
System.out.println("loading 2 ...");
// second load
JSObject dom2Window = (JSObject) webEngine.executeScript("window");
dom2Window.setMember("variable2Name", "variable value 2");
dom2Window.setMember("ctr", this);
// this load does NOT work as expected, variable2Name is NOT set before JavaScript runs and it can NOT be accessed in document ready event
webEngine.load(WebEngineLoadBug.class.getResource("/file2.html").toExternalForm());
}
public void load1() {
System.out.println("loading 1 ...");
// first load
JSObject domWindow = (JSObject) webEngine.executeScript("window");
domWindow.setMember("variableName", "variable value 1");
domWindow.setMember("ctr", this);
// this load works perfectly, variableName is set before JavaScript runs and it can be accessed in document ready event
webEngine.load(WebEngineLoadBug.class.getResource("/file1.html").toExternalForm());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment