我正在使用javascript更新网页上包含动态内容的部分字段。它适用于所有PC浏览器(我使用Firefox,IE和Chrome测试过),但不适用于Android手机和iPhone。手机显示静态内容,但不显示javascript写入的值。
是否与HTTP GET的回复消息格式有关?我没有在回复消息中包含HTTP标头,只包含原始数据。
我的js代码如下:
function get_datetime() {
var GetReq = false;
var hourText = document.getElementById("hour");
var minuteText = document.getElementById("minute");
var dayText = document.getElementById("day");
var monthText = document.getElementById("month");
var yearText = document.getElementById("year");
function GetValComplete() {
if (GetReq.readyState == 4) {
if (GetReq.status == 200) {
var time = GetReq.responseText.split(";");
hourText.value = time[0];
minuteText.value = time[1];
dayText.value = time[2];
monthText.value = time[3];
yearText.value = time[4];
}
}
}
if (window.XMLHttpRequest) {
GetReq = new XMLHttpRequest();
}
else if (window.ActiveXObject) {
GetReq = new ActiveXObject("Microsoft.XMLHTTP");
}
if (GetReq) {
GetReq.open("GET", "/datetime?id=" + Math.random(), true);
GetReq.onreadystatechange = GetValComplete;
GetReq.send(null);
return true;
}
return false;
}
我的HTML代码是:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-9" />
<title>date-time</title>
<script type="text/javascript" src="datetime.js"></script>
<style type="text/css">
.auto-style3 {
border: 1px solid #000000;
width: 100px;
}
</style>
</head>
<body onload="get_datetime();">
<br />
<h2><strong>date-time settings:</strong></h2>
<h2><strong></strong></h2>
<br />
<form id="form5" name="form5" method="post" action="">
<p><label for="hour">Hour</label>
<input name="hour" id="hour" class="auto-style3" type="text" /><label for="minute">Minute</label>
<input name="minute" id="minute" class="auto-style3" type="text" /></p>
</form>
<br />
<form id="form6" name="form6" method="post" action="">
<label for="day">Day</label>
<input name="day" class="auto-style3" id="day" type="text" /><label for="month">Month</label>
<input name="month" class="auto-style3" id="month" type="text" /><label for="year">Year</label>
<input name="year" class="auto-style3" id="year" type="text" /></form>
<br />
</body></html>
答案 0 :(得分:3)
它是缺少的HTTP标头。携带动态内容的HTTP响应没有任何HTTP头。现在我手动添加了标题(我必须手动添加它,因为它是一个定制的系统)并且它工作。
缺少的标题在PC浏览器上无关紧要,但Android / iOS显然不接受。
答案 1 :(得分:0)
我看到你正在使用一个特殊的字符集,这可能是问题吗?
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-9" />
没有理由关闭 - 试着把它拿出来。像这样:
function get_datetime() {
var GetReq = false;
function GetValComplete() {
if (GetReq.readyState == 4) {
if (GetReq.status == 200) {
var time = GetReq.responseText.split(";");
var hourText = document.getElementById("hour");
var minuteText = document.getElementById("minute");
var dayText = document.getElementById("day");
var monthText = document.getElementById("month");
var yearText = document.getElementById("year");
hourText.value = time[0];
minuteText.value = time[1];
dayText.value = time[2];
monthText.value = time[3];
yearText.value = time[4];
}
}
}
if (window.XMLHttpRequest) {
GetReq = new XMLHttpRequest();
}
else if (window.ActiveXObject) {
GetReq = new ActiveXObject("Microsoft.XMLHTTP");
}
if (GetReq) {
GetReq.open("GET", "/datetime?id=" + Math.random(), true);
GetReq.onreadystatechange = GetValComplete;
GetReq.send(null);
return true;
}
return false;
}