Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/main/java/org/htmlunit/html/HtmlPage.java
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,8 @@ public void initialize() throws IOException, FailingHttpStatusCodeException {
executeEventHandlersIfNeeded(Event.TYPE_READY_STATE_CHANGE);
}

executePostponedScriptsIfNeeded();

executeDeferredScriptsIfNeeded();

executeEventHandlersIfNeeded(Event.TYPE_DOM_DOCUMENT_LOADED);
Expand Down Expand Up @@ -1477,6 +1479,13 @@ private String getRefreshStringOrNull() {
return getWebResponse().getResponseHeaderValue("Refresh");
}

private void executePostponedScriptsIfNeeded() {
if (!getWebClient().isJavaScriptEnabled()) {
return;
}
getWebClient().getJavaScriptEngine().processPostponedActions();
}

/**
* Executes any deferred scripts, if necessary.
*/
Expand Down
21 changes: 21 additions & 0 deletions src/test/java/org/htmlunit/html/HtmlPageTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1973,4 +1973,25 @@ public void getBaseUrl() throws Exception {
page = loadPage(getBrowserVersion(), html, null, new URL(URL_FIRST + path));
assertEquals(URL_FIRST.toExternalForm() + path, page.getBaseURL().toExternalForm());
}

/**
* @throws Exception if an error occurs
*/
@Test
public void asyncScriptExecutedWithoutEventHandlers() throws Exception {
final String html = DOCTYPE_HTML
+ "<html><head>\n"
+ "<script async src='async.js'></script>\n"
+ "</head><body></body></html>";

final WebClient client = getWebClient();
final MockWebConnection webConnection = new MockWebConnection();
webConnection.setDefaultResponse(html);
webConnection.setResponse(new URL(URL_FIRST, "async.js"),
"document.title = 'async-executed';", "text/javascript");
client.setWebConnection(webConnection);

final HtmlPage page = client.getPage(URL_FIRST);
assertEquals("async-executed", page.getTitleText());
}
}
Loading