如何在div中显示html内容,以及如何在HTA的html文件中添加内容



我按照这个教程成功创建了一个简单的基于web的聊天应用程序

唯一的区别是我没有包括登录的事情。它只在log.html中生成post消息。

现在我试着在HTA中做这个。这是我第一次使用它。我还在学习,目前到这个:

<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=9" />
<meta charset="utf-8" />
<title>Chat-App</title>
<meta name="description" content="Chat-App" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0" />
<link rel="stylesheet" type="text/css" href="font-awesome-animation.min.css"/>
<link rel="stylesheet" href="styles.css" />

<HTA:APPLICATION 
SCROLL="auto"
SINGLEINSTANCE="yes"
WINDOWSTATE="normal"/>
<script language="VBScript">
Sub window_OnLoad
Window.ResizeTo 680,723
iTimerID = window.setInterval("Display", 1000)
End Sub

strPath = "C:UsersusernameDesktopChat App"
Set wshShell = CreateObject( "WScript.Shell" )
'strSender = wshShell.ExpandEnvironmentStrings( "%USERNAME%" )

Sub Display
Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objFile = objFSO.OpenTextFile(StrPath & "log.html", 1)
strCharacters = objFile.ReadAll


objFile.Close
chatbox.innerHTML = strCharacters
chatbox.ScrollTop = chatbox.ScrollHeight

End Sub
</script>
</head>

<body style="background-color:grey;">
<div id="wrapper">
<h1 style="margin-top: 5px;margin-left:20px;color:orange;">Chat-App</h1>
<div id="chatbox" name="chatbox" class="textbox">
</div>

<form name="message" action="">
<textarea rows="10" cols="30" name="usermsg" type="text" id="usermsg" style="height:200px;" placeholder="Type your message here..."></textarea>
<input name="submitmsg" type="submit" id="submitmsg" value="Send" />
</form>
</div>
</body>
</html>

这是输出…ChatApp

这只得到log.html的内容,然后在chatboxdiv中显示。但它有一个错误说'显示'没有定义。textarea甚至看起来都不像textarea

除此之外,我不知道如何在log.html中发布消息并像教程中那样每隔一秒钟刷新一次。我尝试粘贴在基于web的工作的javascript,但它不工作,当我把它转换为html。有人能帮我在HTA里做一下吗?

官方文档有答案

语法

retVal = object.setInterval(expression, msec, language);
参数

  • expression [in] Type: VARIANT

    指向变量的指针,该变量指定函数指针或string对象指定时间间隔为时执行的代码运行。

  • msec [in]类型:long

    long指定毫秒数。

  • language [in, optional] Type: VARIANT

    指向BSTR的指针,该BSTR指定了对象的任何一个可能值

这里重要的参数是language,因为它告诉setInterval(),expression是用特定的语言(JScript, VBScript等)编写的。

换行:

iTimerID = window.setInterval("Display", 1000)

:

iTimerID = window.setInterval("Display", 1000, "VBScript")

这是一个简单的HTA聊天框应用程序,您可以从中制作自己的版本。您可以在同一台机器上运行多个副本,或者将聊天日志文件设置为共享驱动器上的一个路径,并在多台计算机上运行。

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" http-equiv="X-UA-Compatible" content="IE=9">
<hta:application
id=oHTA
icon=notepad.exe
applicationname=ChatBox
selection=yes
singleinstance=no
>
<script>    
function Refresh() {
Iframe = document.getElementById("chatbox").contentWindow;
window.setInterval(function() {
fso = new ActiveXObject("Scripting.FileSystemObject");
fh = fso.OpenTextFile(ChatLog,1,1);
FileContents = fh.AtEndOfStream ? "" : fh.ReadAll();
fh.Close();
Iframe.document.body.innerHTML = FileContents
if (autoscroll.checked) {Iframe.scrollTo( 0,999999)};
}, 500);
}
function replaceAll(str, match, replacement){
return str.split(match).join(replacement);
}
function PostMsg() {
TrimMsg = replaceAll(msg.value.trim(),'n','<br>');
if (TrimMsg.length > 0) {
fh = fso.OpenTextFile(ChatLog,8);
UserName = user.value.trim();
if (UserName=='') {UserName = 'Anonymous'};
fh.write('<button>' + UserName + '</button><br><div>' + TrimMsg + '</div><br>');
fh.Close();
}
}
</script>
<style>
body {background-color:grey}
#chatbox {background-color:white; height:30em; width:30em}
#sb {float:right}
</style>
</head>
<body onload=Refresh()>
<script>
document.title = "Chat Box"
ChatLog = ".\Chat.txt";
x = 520;
y = 680;
window.resizeTo(x, y);
window.moveTo((screen.availWidth - x)/2, (screen.availHeight - y)/2);
</script>
<iframe id=chatbox title="Chat Box"></iframe><br><br>
<textarea id=msg rows=4 cols=58></textarea><br><br>
Screen name:<input type=text id=user>
Autoscroll:<input type=checkbox checked id=autoscroll>
<input type=button id=sb value=Send OnClick=PostMsg()>
</body>
</html>

最新更新