diff --git a/src/main/java/org/htmlunit/WebClient.java b/src/main/java/org/htmlunit/WebClient.java
index 640ac567b62..e9c3f357d0d 100644
--- a/src/main/java/org/htmlunit/WebClient.java
+++ b/src/main/java/org/htmlunit/WebClient.java
@@ -307,7 +307,7 @@ public WebClient(final BrowserVersion browserVersion, final boolean javaScriptEn
// The window must be constructed AFTER the script engine.
currentWindowTracker_ = new CurrentWindowTracker(this, true);
- currentWindow_ = new TopLevelWindow("", this);
+ new TopLevelWindow("", this);
initMSXMLActiveX();
}
@@ -2233,8 +2233,7 @@ public void afterWebWindowClosedListenersProcessed(final WebWindowEvent event) {
if (webClient_.topLevelWindows_.isEmpty()) {
// Must always have at least window, and there are no top-level windows left; must create one.
- final TopLevelWindow newWindow = new TopLevelWindow("", webClient_);
- webClient_.setCurrentWindow(newWindow);
+ new TopLevelWindow("", webClient_);
}
}
@@ -2286,6 +2285,7 @@ public void webWindowOpened(final WebWindowEvent event) {
if (window instanceof TopLevelWindow) {
final TopLevelWindow tlw = (TopLevelWindow) window;
webClient_.topLevelWindows_.add(tlw);
+ webClient_.setCurrentWindow(tlw);
}
// Page is not loaded yet, don't set it now as current window.
}
@@ -2436,7 +2436,7 @@ public void reset() {
// The window must be constructed AFTER the script engine.
currentWindowTracker_ = new CurrentWindowTracker(this, true);
- currentWindow_ = new TopLevelWindow("", this);
+ new TopLevelWindow("", this);
}
/**