有一种方法——我能想到——可以做到这一点,但这真的很痛苦。就像提姆提到的那样。但是,如果您坚持使用单个 hta 文件,这是您的代码:
<html>
<head>
<title>Main HTA</title>
<HTA:APPLICATION
SCROLL="no"
INNERBORDER="no"
/>
</head>
<body>
This is the main HTA <br /><br />
<input type="button" value="open sub hta" onclick="runSubHTA(subhta)" />
<script type="text/javascript">
// path to the new (sub) hta file
var subhta = "subhta.hta";
// function to create and launch the sub hta file
function runSubHTA(path) {
// creating the new hta file
var fso = new ActiveXObject("Scripting.FileSystemObject");
var sub = fso.CreateTextFile(subhta, true);
// writing to the created hta file
sub.WriteLine("<title>Sub HTA<\/title>");
sub.WriteLine("Welcome to the sub HTA <br />");
// this code will run on the sub hta before exit
// and it'll delete the sub hta file, meaning itself
sub.WriteLine('<script type="text/javascript">');
sub.WriteLine('var subhta = "subhta.hta";');
sub.WriteLine('window.onbeforeunload = function() {');
sub.WriteLine('var fso = new ActiveXObject("Scripting.FileSystemObject");');
sub.WriteLine('var sub = fso.DeleteFile(subhta, 1);');
sub.WriteLine('}<\/script>');
// closing the connection to the file, we're done writing
sub.Close();
// launching the sub hta file
WshShell = new ActiveXObject("WScript.Shell");
WshShell.Run(path, 1, false);
}
</script>
</body>
</html>
编码 2 个单独的 hta 文件会容易得多。这样,您必须逐行嵌入您的 sub hta 代码。但可能有更好的方法来做到这一点。就像用换行和缩进将整个页面写在一行中一样。我以前没有尝试过这样的事情,所以我对此无能为力。但是,如果您找到了方法,请分享。