Created
March 8, 2016 08:31
-
-
Save anonymous/03f8f78181a8159f443f to your computer and use it in GitHub Desktop.
Attempt to reproduce a WebEngine loading bug in JavaFX (failed)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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> | |
<script> | |
$(document).ready(function() { | |
// use variableName here | |
$("#title").html(variableName); | |
}); | |
</script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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> | |
<script> | |
$(document).ready(function() { | |
// use variableName here | |
$("#title").html(variableName); | |
}); | |
</script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javafx.application.Application; | |
import javafx.scene.Scene; | |
import javafx.scene.web.WebEngine; | |
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 | |
{ | |
/** | |
* @param args ignored. | |
*/ | |
public static void main( final String[] args ) | |
{ | |
launch( args ); | |
} | |
@Override | |
public void start( final Stage primaryStage ) throws Exception | |
{ | |
final WebView webView = new WebView(); | |
primaryStage.setScene( new Scene( webView ) ); | |
final WebEngine webEngine = webView.getEngine(); | |
// first load | |
final JSObject domWindow = (JSObject) webEngine.executeScript( "window" ); | |
domWindow.setMember( "variableName", "variable value 1" ); | |
// 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() ); | |
// second load | |
final JSObject dom2Window = (JSObject) webEngine.executeScript( "window" ); | |
dom2Window.setMember( "variableName", "variable value 2" ); | |
// 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() ); | |
primaryStage.show(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment