2014-06-27 37 views
0

我有一個功能,檢查移動互聯網。改變從點擊到開啓加載的活動功能

這個功能點擊工作(OnClick Function) 我會在應用程序啓動時自動工作!

我該怎麼辦?

的功能是這樣的:

<script> 
function checkJSNetConnection(){ 
var xhr = new XMLHttpRequest(); 
var file = "dot.png"; 
var r = Math.round(Math.random() * 10000); 
xhr.open('HEAD', file + "?subins=" + r, false); 
try { 
    xhr.send(); 
    if (xhr.status >= 200 && xhr.status < 304) { 
    return true; 
    } else { 
    return false; 
    } 
} catch (e) { 
    return false; 
} 
} 

    function onJSButtonclick(){ 
    if(checkJSNetConnection()==true){ 
     alert("Internet Connection Exists"); 
    }else{ 
     alert("Internet Connection Doesn't Exist"); 
    } 
    } 
    </script> 

回答

0

嘗試:

$(document).ready(function(){ 
    onJSButtonclick(); 
}); 

只是你結束</script>標記之前添加它。

+0

偉大的:) ....... – user3682743

0

使用document.onload = function() {...};或者,如果您有jQuery的,使用`$(文件)。就緒(函數(){...});

例如使用這個腳本看起來像

document.onload = function() { 
    if (checkJSNetConnection() === true) { 
    alert("Internet Connection Exists"); 
    } else { 
    alert("Internet Connection Doesn't Exist"); 
    } 
}; 

$(document).ready(function() { 
    if (checkJSNetConnection() === true) { 
    alert("Internet Connection Exists"); 
    } else { 
    alert("Internet Connection Doesn't Exist"); 
    } 
}); 
+0

你可以用我的代碼添加一個例子嗎?請 – user3682743

+0

感謝工廠.... – user3682743