简体   繁体   中英

window.close not closing window in HTA application

In my HTA application I'm using a JavaScript calendar window, it opens using window.open() and closed using window.close(), when the user clicks on one of the dates. This calendar works fine on multiple browsers and versions over more than 10 years. It even works in HTA applications most of the time.

However on specific workstations running IE11. The window.close() command is simply ignored, resulting in the window left open. On other IE11 workstations it works fine. I figured that turning off the "Enable Protected Mode" checkbox on IE11, Internet Options, Security tab resolves the problem on one of the problematic workstation. However, other workstations works fine with this setting turned on and turning off this setting is not an acceptable solution.

Code sample which reproduces the problem:

HTA application

<HTML>
<HEAD>
<HTA:APPLICATION ID="OpenCloseExample" BORDER="thick" BORDERSTYLE="complex"/>
<TITLE>Open Close HTA container</TITLE>
</HEAD>
<iframe width="1024px" height="768px" src="http://localhost:28080/openclose.html"/>
</HTML>

openclose.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Main Page</title>
    <script src="openclose.js"></script>
</head>
<body>
    <a href="#" onclick="openWindow();">open</a>
</body>
</html>

openclose.js

var win;

function openWindow() {
    win = window.open("", "_blank", 'width=250,height=250,status=no,resizable=no,location=no,top=100,left=100');
    win.document.writeln("<html><head><script src='openclose.js'></script></head><a href='#' onclick='javascript:window.opener.closeWindow()'>close</a></html>");
}

function closeWindow() {
    win.window.close();
}

I can't see this working in any IE with any settings. The problem is this string: <script src='openclose.js'></script> . That is, a literal ending script tag in a string works as an ending script tag on a page, when HTML parser will find it. This means, that your script was never loaded.

To fix this, you've to break the literal tag, for example like so:

<script src='openclose.js'><\\/script>

I figured this out eventually. Changing:

<a href="#" onclick="openWindow();">open</a>

to:

<a href="javascript:openWindow()">open</a>

Has resolved the problem

Since you have pointed out that IE11 is causing the JS not to work, you can force IE to render in an older version very easily.

<meta http-equiv="X-UA-Compatible" content="IE=9">

This meta tag is very popular amongst HTA applications for utilizing JS/ActiveX methods/properties within specific IE versions (most of them being deprecated).

For more information, visit the X-UA-Compatible Tag Wiki

Hope this helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM