我有这段代码:
<html>
<head>
<title>Messenger</title>
</head>
<body onload="checkcookie(); update();">
<div id="whitebg"></div>
<div id="loginbox">
<h1>Pick a username:</h1>
<p><input type="text" name="pickusername" id="cusername" placeholder="Pick a username" class="msginput"></p>
<p class="buttonp"><button onclick="chooseusername()">Choose Username</button></p>
</div>
<div class="msg-container">
<div class="header">Messenger</div>
<div class="msg-area" id="msg-area"></div>
<div class="bottom"><input type="text" name="msginput" class="msginput" id="msginput" onkeydown="if (event.keyCode == 13) sendmsg()" value="" placeholder="Enter your message here ... (Press enter to send message)"></div>
</div>
<script type="text/javascript">
var msginput = document.getElementById("msginput");
function showLogin() {
document.getElementById("whitebg").style.display = "inline-block";
document.getElementById("loginbox").style.display = "inline-block";
}
function hideLogin() {
document.getElementById("whitebg").style.display = "none";
document.getElementById("loginbox").style.display = "none";
}
function chooseusername() {
var user = document.getElementById("cusername").value;
document.cookie = "messengerUname" + user;
checkcookie();
}
function checkcookie() {
if(document.cookie.indexOf("messageUname") == -1) {
showLogin();
} else {
hideLogin();
}
}
function getcookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for(var i=0; i<ca.length; i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1);
if (c.indexOf(name) == 0) return c.substring(name.length,c.length);
}
return "";
}
function sendmsg() {
var message = msginput.value;
if(message != "") {
var username = getcookie("messengerUname");
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
console.log(xmlhttp.responseText);
}
}
xmlhttp.open("GET","update-messages.php?username="+username+"&message="+message, true);
xmlhttp.send();
}
}
</script>
</body>
</html>
我遇到的问题是,当我设置用户名并单击“选择用户名”按钮时,hideLogin()函数不会执行。您可以看到网站here
我正在使用000webhost来托管它。另一个问题是,当我在控制台中键入sendmsg()
时,它表示sendmsg未定义
答案 0 :(得分:0)
据我所知,为什么会执行?
您不要在选择登录功能中调用它,也不要在Cookie中写"messageUname"
虽然可能存在错误:
if(document.cookie.indexOf("messageUname") == -1) {
由于您的所有其他功能都提及"messengerUname"
,我认为这是一个简单的错字。
将"messageUname"
替换为"messengerUname"
,您应该很好
(从未做过javascript,所以我可能错了)