Skip to content

Instantly share code, notes, and snippets.

@bugabinga
Forked from anonymous/WebEngineLoadBug.java
Created March 8, 2016 08:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bugabinga/c29fb08cda9d5c21e4e1 to your computer and use it in GitHub Desktop.
Save bugabinga/c29fb08cda9d5c21e4e1 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>
<script>
$(document).ready(function() {
// use variableName here
$("#title").html(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>
<script>
$(document).ready(function() {
// use variableName here
$("#title").html(variableName);
});
</script>
</body>
</html>
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