2015-02-11 73 views
1

我想要一個表單在沒有互聯網連接時返回false。我有腳本,檢查頁面加載時的互聯網連接,它工作正常。但是,當用戶提交併且沒有互聯網時,它會顯示警報權限,但仍會嘗試提交,並會顯示默認錯誤網頁「無法找到該頁面」。當沒有互聯網連接時返回False

<form action="http://usersite.com/script.php" method="post" name="form1" target="_parent" id="form1" onsubmit="checkConnection()"> 

檢查Internet

<script type="text/javascript" src="cordova-2.5.0.js"></script> 
<script type="text/javascript" charset="utf-8"> 

// Wait for PhoneGap to load 
// 
document.addEventListener("deviceready", onDeviceReady, false); 

// PhoneGap is loaded and it is now safe to make calls PhoneGap methods 
// 
function onDeviceReady() { 
    checkConnection(); 
} 

function checkConnection() { 
    var networkState = navigator.network.connection.type; 

    var states = {}; 
    states[Connection.UNKNOWN] = 'Unknown connection'; 
    states[Connection.ETHERNET] = 'Ethernet connection'; 
    states[Connection.WIFI]  = 'WiFi connection'; 
    states[Connection.CELL_2G] = 'Cell 2G connection'; 
    states[Connection.CELL_3G] = 'Cell 3G connection'; 
    states[Connection.CELL_4G] = 'Cell 4G connection'; 
    states[Connection.NONE]  = 'No network connection'; 

    if ((states[networkState]) == states[Connection.NONE]) 

    alert('No Internet Connection'); 
    return false; 
} 

</script> 
+0

參考這一點,它可以幫助你: [http://stackoverflow.com/questions/28465524/phonegap-no-internet-alert-彈出發出] [1] [1]:http://stackoverflow.com/questions/28465524/phonegap-no-internet-alert-pop-up-issue – Amar1989 2015-02-12 08:29:15

回答

1
Define Enum 

`var NetworkStatusEnum = { 
NO_INTERNET_DEVICE : {status:StatusCodes.NO_INTERNET_DEVICE,message:"No Internet connection on the device"}, 
NETWORK_OK : {status:StatusCodes.NETWORK_FINE,message:"Network fine"} 
}` 

Write following method 

`function isNetworkPresent() 
{ 
     var response = {}; 

     var networkState = navigator.connection.type; 
     var states = {}; 
     states[Connection.UNKNOWN] = false; 
     states[Connection.ETHERNET] = true; 
     states[Connection.WIFI]  = true; 
     states[Connection.CELL_2G] = true; 
     states[Connection.CELL_3G] = true; 
     states[Connection.CELL_4G] = true; 
     states[Connection.CELL]  = true; 
     states[Connection.NONE]  = false; 
     if (states[networkState]) { 
      response = NetworkStatusEnum["NETWORK_OK"]; 

     } else { 
      response = NetworkStatusEnum["NO_INTERNET_DEVICE"]; 
     } 

     return response; 
}` 

Now wherever you want to check for internet call `isNetworkPresent` method 

var networkResponse = isNetworkPresent(); 
if(networkResponse.status == StatusCodes.NETWORK_FINE) 
{ 
    //Your code on internet present 
} 
else 
{ 
    //Your code on internet not present 
} 
相關問題