public class HomeController : Controller
{
//
// GET: /Home/
public ActionResult Index()
{
return View();
}
}
@{
Layout = null;
}
!DOCTYPE html>
html>
head>
title>Index/title>
script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js">/script>
script language="javascript">
var req = false;
var lastDelimiterPosition = -1;
$(document).ready(function () {
getData();
});
function getData() {
loadXMLDoc("/Comet/Test");
}
//新建一個XHR
function createRequest() {
if (window.XMLHttpRequest !(window.ActiveXObject)) {
try {
req = new XMLHttpRequest();
} catch (e) {
req = false;
} // branch for IE/Windows ActiveX version
} else if (window.ActiveXObject) {
try { req = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {
try {
req = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {
req = false;
}
}
}
}
//發(fā)起請求
function loadXMLDoc(url) {
try {
if (req) {
req.abort();
req = false;
}
createRequest();
if (req) {
req.onreadystatechange = processReqChange;
req.open("GET", url, true);
req.send("");
} else {
alert('unable to create request');
}
} catch (e) { alert(e.message); }
}
//檢查狀態(tài)
function processReqChange() {
if (req.readyState == 3) {
try {
ProcessInput(req.responseText);
if (req.responseText.length > 3000) {
lastDelimiterPosition = -1; getData();
}
}
catch (e) {
alert(e.message);
}
}
}
//拆分字符串
function ProcessInput(input) {
var text = input;
var nextDelimiter = text.indexOf('|', lastDelimiterPosition + 1);
if (nextDelimiter != -1) {
var timeStamp = text.substring(nextDelimiter + 1);
if (timeStamp.length > 0) {
lastDelimiterPosition = nextDelimiter;
ProcessTime(timeStamp);
}
}
}
//輸出 或者觸發(fā)什么事件
function ProcessTime(time) {
document.getElementById('div1').innerHTML = time;
}
/script>
/head>
body>
div>
div id="div1">
/div>
div id="div2">
/div>
/div>
/body>
/html>
實際上還可以通過iframe> 和 script>這兩個標(biāo)簽做實現(xiàn),特別是script標(biāo)簽可以訪問和執(zhí)行跨域的javascript