2017-06-14 8 views
1

Ich bin so neu für Ajax und Jquery!Ajax Anfrage funktioniert nicht für mobile Geräte

Im Folgenden arbeiten meine Codes als ein Charme bei Desktop-PCs.

Ich kann die verbrachte Zeit für Kunden sammeln, aber in mobilen Geräten funktioniert eine Ajax-Anfrage nicht.

Hier ist mein Code:

<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> 

Es funktioniert nicht für mein Handy (iPhone)!

Jeder könnte mir bitte helfen?

Danke!

+0

Können Sie bestätigen, auf welchen mobilen Geräten Sie testen? – mjw

+0

Nur Iphone 6, Apple-Gerät, habe ich getestet – Bagova

+0

Sie beantwortet es in der Frage (iPhone). – mjw

Antwort

0

Ich denke, dieses Problem wurde durch die Tatsache verursacht, dass das mobile Gerät window.onbeforeunload nicht unterstützt. Um ähnliche Wirkung zu haben, versuchen document.unload oder document.pagehide

0

Versuchen Sie folgendes:

<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> 

Dieses Ereignis wird beide arbeiten für Browser und iOS-Geräte als auch.

+0

@Bagova Funktioniert diese Lösung für Sie? Lass es mich wissen, damit es auch anderen hilft. Vielen Dank –

Verwandte Themen