第二个ajax请求在移动浏览器上不起作用
second ajax request not working on mobile browsers
我正试图用自己的Web服务器建立一个气象站。到目前为止,页面的代码是这样的:
<!DOCTYPE html>
<html>
<head>
<title>WS-100 Server</title>
<script>
function UpdatePage()
{
var request = new XMLHttpRequest();
request.onreadystatechange = function()
{
if(this.readyState == 4) {
if(this.status == 200) {
if(this.responseText != null) {
document.getElementById("currentTemp").innerHTML = this.responseText;
}
}
}
}
request.open("GET","ajax_update_temp",true);
request.send(null);
request= new XMLHttpRequest();
request.onreadystatechange = function()
{
if(this.readyState == 4) {
if(this.status == 200) {
if(this.responseText != null) {
document.getElementById("currentDatetime").innerHTML = this.responseText;
}
}
}
}
request.open("GET","ajax_update_time",true);
request.send(null);
setTimeout("UpdatePage()", 10000);
}
</script>
</head>
<body onload="UpdatePage()">
<h1>WS-100 Weather Station</h1>
<div id="currentDatetime">
<p> </p>
</div>
<div id="currentTemp">
<p> </p>
</div>
</body>
</html>
它在桌面浏览器上运行得很好,但在移动浏览器上只显示日期时间,在我的旧三星galaxy上,没有显示。我曾考虑将这两个查询合并为一个,但最终我想至少做两个请求:一个是当前时间和温度,另一个是下载并处理历史测量的txt文件。
这只是浏览器的限制吗?还是应该有办法解决它?
尝试运行$('document').ready(function());
,因为在移动Web视图中,桌面浏览器的某些功能为了保持其性能而受到限制。因此,有时您需要编写$('document').ready(function());
而不是onload()
来自Prabhu答案和我的答案:
<!DOCTYPE html>
<html>
<head>
<title>WS-100 Server</title>
<script>
function UpdatePage()
{
var request = new XMLHttpRequest();
request.onreadystatechange = function()
{
if(this.readyState == 4) {
if(this.status == 200) {
if(this.responseText != null) {
document.getElementById("currentTemp").innerHTML = this.responseText;
}
}
}
}
request.open("GET","ajax_update_temp",true);
request.send(null);
var request2= new XMLHttpRequest();
request2.onreadystatechange = function()
{
if(this.readyState == 4) {
if(this.status == 200) {
if(this.responseText != null) {
document.getElementById("currentDatetime").innerHTML = this.responseText;
}
}
}
}
request2.open("GET","ajax_update_time",true);
request2.send(null);
setTimeout("UpdatePage()", 10000);
}
$('document').ready(function() {
UpdatePage();
});
</script>
</head>
<body>
<h1>WS-100 Weather Station</h1>
<div id="currentDatetime">
<p> </p>
</div>
<div id="currentTemp">
<p> </p>
</div>
</body>
</html>
相关文章:
- 当我在浏览器中打开HTML文件时,javascript不起作用
- Javascript的某些部分在Chrome中不起作用,但在其他浏览器中可以完美工作
- 为什么简单的Promise语句在浏览器中不起作用
- 用于播放背景音乐的嵌入式flash播放器在IE浏览器中不起作用
- 带有Safari的Javascript;不起作用——所有其他浏览器都起作用
- 使用Javascript的Ajax请求在iPhone浏览器中不起作用
- 谷歌浏览器模式正则表达式在使用setCustomValidity动态创建表单时不起作用
- 为什么我的谷歌浏览器扩展程序在谷歌浏览器版本 41.0.2272.89 中不起作用
- 为什么这个JavaScript在浏览器中不起作用
- 多个 socket.io 客户端连接不起作用(谷歌浏览器,火狐浏览器)
- 脚本在web浏览器控件中不起作用
- 禁用浏览器中的后退按钮不起作用
- 将JSON对象转换为敲除observable在IE8中不起作用,但在所有其他浏览器中都起作用
- jQuery在我的网站上不起作用,但在我的浏览器上起作用
- 角度路由;在浏览器中直接访问URL时不起作用,但在单击时起作用
- 转到顶部按钮在Firefox浏览器中不起作用
- javascript代码在浏览器中不起作用
- JavaScript文件在浏览器中不起作用,但它在代码笔中起作用
- Javascript浏览器通知不起作用
- 浏览器关闭事件 对于火狐浏览器不起作用