Jquery Ajax请求没有';不适用于移动设备
我对ajax和jquery非常陌生 在下面,我的代码在台式电脑上很有魅力 我可以收集客户端花费的时间,但在移动设备中ajax请求不起作用 这是我的密码:Jquery Ajax请求没有';不适用于移动设备,jquery,ajax,xmlhttprequest,ajax-request,Jquery,Ajax,Xmlhttprequest,Ajax Request,我对ajax和jquery非常陌生 在下面,我的代码在台式电脑上很有魅力 我可以收集客户端花费的时间,但在移动设备中ajax请求不起作用 这是我的密码: <script type="text/javascript"> var startTime = new Date(); //Start the clock! window.onbeforeunload = function() //When the user leaves the page(closes
<script type="text/javascript">
var startTime = new Date(); //Start the clock!
window.onbeforeunload = function() //When the user leaves the page(closes the window/tab, clicks a link)...
{
var endTime = new Date(); //Get the current time.
var timeSpent = (endTime - startTime); //Find out how long it's been.
var xmlhttp; //Make a variable for a new ajax request.
if (window.XMLHttpRequest) //If it's a decent browser...
{
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest(); //Open a new ajax request.
}
else //If it's a bad browser...
{
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); //Open a different type of ajax call.
}
var url = "http://www.example.com/time.php?time="+timeSpent; //Send the time on the page to a php script of your choosing.
xmlhttp.open("GET",url,false); //The false at the end tells ajax to use a synchronous call which wont be severed by the user leaving.
xmlhttp.send(null); //Send the request and don't wait for a response.
}
</script>
var startTime=新日期()//开始计时!
window.onbeforeunload=function()//当用户离开页面时(关闭窗口/选项卡,单击链接)。。。
{
var endTime=new Date();//获取当前时间。
var timespunt=(endTime-startTime);//了解它已经运行了多长时间。
var xmlhttp;//为新的ajax请求创建一个变量。
if(window.XMLHttpRequest)//如果它是一个不错的浏览器。。。
{
//IE7+、Firefox、Chrome、Opera、Safari的代码
xmlhttp=newXMLHttpRequest();//打开一个新的ajax请求。
}
否则//如果浏览器不好。。。
{
//IE6、IE5的代码
xmlhttp=newActiveXObject(“Microsoft.xmlhttp”);//打开不同类型的ajax调用。
}
变量url=”http://www.example.com/time.php?time=“+timespunt;//将页面上的时间发送到您选择的php脚本。
open(“GET”,url,false);//最后的false告诉ajax使用同步调用,用户离开时不会中断该调用。
xmlhttp.send(null);//发送请求,不要等待响应。
}
它不适用于我的手机(iphone)
有人能帮我吗
谢谢 我认为这个问题是由于移动设备不支持
window.onbeforeunload
造成的。要获得类似效果,请尝试文档。卸载或文档。页面隐藏尝试以下操作:
<script type="text/javascript">
var startTime = new Date(); //Start the clock!
var isOnIOS = navigator.userAgent.match(/iPad/i)|| navigator.userAgent.match(/iPhone/i);
var eventName = isOnIOS ? "pagehide" : "beforeunload";
window.addEventListener(eventName, function (event) {
var endTime = new Date(); //Get the current time.
var timeSpent = (endTime - startTime); //Find out how long it's been.
var xmlhttp; //Make a variable for a new ajax request.
if (window.XMLHttpRequest) //If it's a decent browser...
{
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest(); //Open a new ajax request.
}
else //If it's a bad browser...
{
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); //Open a different type of ajax call.
}
var url = "http://www.example.com/time.php?time="+timeSpent; //Send the time on the page to a php script of your choosing.
xmlhttp.open("GET",url,false); //The false at the end tells ajax to use a synchronous call which wont be severed by the user leaving.
xmlhttp.send(null); //Send the request and don't wait for a response.
});
</script>
var startTime=新日期()//开始计时!
var isOnIOS=navigator.userAgent.match(/iPad/i)| | navigator.userAgent.match(/iPhone/i);
var eventName=isOnIOS?“页面隐藏”:“卸载前”;
addEventListener(事件名称,函数(事件){
var endTime=new Date();//获取当前时间。
var timespunt=(endTime-startTime);//了解它已经运行了多长时间。
var xmlhttp;//为新的ajax请求创建一个变量。
if(window.XMLHttpRequest)//如果它是一个不错的浏览器。。。
{
//IE7+、Firefox、Chrome、Opera、Safari的代码
xmlhttp=newXMLHttpRequest();//打开一个新的ajax请求。
}
否则//如果浏览器不好。。。
{
//IE6、IE5的代码
xmlhttp=newActiveXObject(“Microsoft.xmlhttp”);//打开不同类型的ajax调用。
}
变量url=”http://www.example.com/time.php?time=“+timespunt;//将页面上的时间发送到您选择的php脚本。
open(“GET”,url,false);//最后的false告诉ajax使用同步调用,用户离开时不会中断该调用。
xmlhttp.send(null);//发送请求,不要等待响应。
});
<>这个事件对浏览器和iOS设备都适用。你能确认你正在测试什么移动设备吗?只有iPhone 6,苹果设备,我测试你在问题(iPhone)中回答它。如果答案解决了你的问题,考虑接受答案。下面是如何返回此处并对勾号/复选标记执行相同操作,直到其变为绿色。这将通知社区,找到了解决方案。否则,其他人可能会认为问题仍然悬而未决,并可能希望发布(更多)答案。您将获得积分,并鼓励其他人帮助您。欢迎来到Stack@巴戈娃:这个解决方案适合你吗?让我知道,这样也能帮助别人。谢谢