以下是我使用PHP和jquery编写的代码。这是我的网站http://bubblewschat.com。我遇到的部分问题是代码出现在发布的网站上。我如何删除它?是否可以严格使用html构建聊天室?
<div id="page-wrap">
<h2>Let's talk</h2>
<p id="name-area"></p>
<div id="chat-wrap">
<div id="chat-area"></div>
</div>
<form id="send-message-area">
<p>Your message: </p>
<textarea id="sendie" maxlength="100"></textarea>
</form>
</div>function Chat () { this.update = updateChat; this.send = sendChat; this.getState = getStateOfChat;}//gets the state of the chatfunction getStateOfChat() { if(!instanse){ instanse = true; $.ajax({ type: "POST", url: "process.php", data: {'function': 'getState', 'file': file}, dataType: "json", success: function(data) {state = data.state;instanse = false;} }); } }//Updates the chatfunction updateChat() { if(!instanse){ instanse = true; $.ajax({ type: "POST", url: "process.php", data: {'function': 'update','state': state,'file': file}, dataType: "json", success: function(data) { if(data.text){ for (var i = 0; i < data.text.length; i++) { $('#chat-area').append($(" "+ data.text[i] +" ")); } } document.getElementById('chat-area').scrollTop = document.getElementById('chat-area').scrollHeight; instanse = false; state = data.state; } }); } else { setTimeout(updateChat, 1500); }}//send the messagefunction sendChat(message, nickname) { updateChat(); $.ajax({ type: "POST", url: "process.php", data: {'function': 'send','message': message,'nickname': nickname,'file': file}, dataType: "json", success: function(data){ updateChat(); } });}
<!--?php $function = $_POST['function']; $log = array(); switch($function) { case('getState'): if (file_exists('chat.txt')) { $lines = file('chat.txt'); } $log['state'] = count($lines); break; case('update'): $state = $_POST['state']; if (file_exists('chat.txt')) { $lines = file('chat.txt'); } $count = count($lines); if ($state == $count){ $log['state'] = $state; $log['text'] = false; } else { $text= array(); $log['state'] = $state + count($lines) - $state; foreach ($lines as $line_num =--> $line) { if ($line_num >= $state){ $text[] = $line = str_replace("n", "", $line); } } $log['text'] = $text; } break; case('send'): $nickname = htmlentities(strip_tags($_POST['nickname'])); $reg_exUrl = "/(http|https|ftp|ftps)://[a-zA-Z0-9-.]+.[a-zA-Z]{2,3}(/S*)?/"; $message = htmlentities(strip_tags($_POST['message'])); if (($message) != "n") { if (preg_match($reg_exUrl, $message, $url)) { $message = preg_replace($reg_exUrl, '
<a href="'.$url[0].'" target="_blank">'.$url[0].'</a>', $message); } fwrite(fopen('chat.txt', 'a'), "
<span>". $nickname . "</span>" . $message = str_replace("n", " ", $message) . "n"); } break; } echo json_encode($log);?>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script src="chat.js"></script>
<script> // ask user for name with popup prompt var name = prompt("Enter your chat name:", "Guest"); // default name is 'Guest' if (!name || name === ' ') { name = "Guest"; } // strip tags name = name.replace(/(<([^>]+)>)/ig,""); // display name on page $("#name-area").html("You are: <span>" + name + "</span>"); // kick off chat var chat = new Chat(); $(function() { chat.getState(); // watch textarea for key presses $("#sendie").keydown(function(event) { var key = event.which; //all keys including return. if (key >= 33) { var maxLength = $(this).attr("maxlength"); var length = this.value.length; // don't allow new content if length is maxed out if (length >= maxLength) { event.preventDefault(); } } }); // watch textarea for release of key press $('#sendie').keyup(function(e) { if (e.keyCode == 13) { var text = $(this).val(); var maxLength = $(this).attr("maxlength"); var length = text.length; // send if (length <= maxLength + 1) { chat.send(text, name); $(this).val(""); } else { $(this).val(text.substring(0, maxLength)); } } }); });</script>
Is it possible to build a chatroom strictly with html?
没有。
聊天室是一个交互式网站,它接受多个用户的输入并将其显示给多个用户。因此,它需要一些能够做到这一点的代码。
html不是一种编程语言。这是一种标记语言,即它有代码可以用来告诉浏览器"这段文字是一段文字"。。。但这只是它的范围。
要编写聊天室,您需要使用PHP/javascript/ruby等……这意味着它必须由一个服务器/浏览器运行,该服务器/浏览器实际上运行PHP。。。
$url[0]您必须将$variables
声明包装在php标记中,如下所示<?php
申报?>
这将告诉您的服务器将代码解释为Php。关于您的问题,您可以使用Html AND Ajax(异步JavaScript和XML)建立一个聊天室。
由于php是在服务器端呈现的,您将需要Ajax来调用发布消息、轮询接收到的消息以及显示消息。