㈠ 使用JS在瀏覽器中判斷當前網路連接狀態的幾種方法
通過navigator.onLine判斷當前網路狀態:
if(navigator.onLine){
...}else{ ...}
非常簡單,但是並不準確-根據MDN的描述:
navigator.onLine只會在機器未連接到區域網或路由器時返回false,其他情況下均返回true。
也就是說,機器連接上路由器後,即使這個路由器沒聯通網路,navigator.onLine仍然返回true。
<script type="text/javascript">
function browserRedirect() {
var sUserAgent = navigator.userAgent.toLowerCase();
var bIsIpad = sUserAgent.match(/ipad/i) == "ipad";
var bIsIphoneOs = sUserAgent.match(/iphone os/i) == "iphone os";
var bIsMidp = sUserAgent.match(/midp/i) == "midp";
var bIsUc7 = sUserAgent.match(/rv:1.2.3.4/i) == "rv:1.2.3.4";
var bIsUc = sUserAgent.match(/ucweb/i) == "ucweb";
var bIsAndroid = sUserAgent.match(/android/i) == "android";
var bIsCE = sUserAgent.match(/windows ce/i) == "windows ce";
var bIsWM = sUserAgent.match(/windows mobile/i) == "windows mobile";
if (!(bIsIpad || bIsIphoneOs || bIsMidp || bIsUc7 || bIsUc || bIsAndroid || bIsCE || bIsWM) ){
window.location.href=B頁面;
}
}
browserRedirect(); </script>
㈢ jquery或者js如何判斷當前網路是否暢通,我的頁面是給手機用的
代碼邏輯如下:
jQuery.ajax({
url:"",
dataType:"",
type:"",
success:function(data){
//...調用成功代碼
},
error:function(XMLHttpRequest,textStatus,errorThrown){
alert(XMLHttpRequest.status);//查看請求狀態
//調用失敗代碼
}
});
通過ajax的error參數判斷當前網路狀況。
㈣ js判斷手機是wifi還是流量
以下代碼僅供參考:
<scripttype="text/javascript">
varconnection=navigator.connection||navigator.mozConnection||navigator.webkitConnection||{tyep:'unknown'};
vartype_text=['unknown','ethernet','wifi','2g','3g','4g','none'];
alert(connection.type);
varre_el=document.getElementById("re");
varbtn_el=document.getElementById("btn");
functionget_status(){
if(typeof(connection.type)=="number"){
connection.type_text=type_text[connection.type];
}else{
connection.type_text=connection.type;
}
if(typeof(connection.bandwidth)=="number"){
if(connection.bandwidth>10){
connection.type='wifi';
}elseif(connection.bandwidth>2){
connection.type='3g';
}elseif(connection.bandwidth>0){
connection.type='2g';
}elseif(connection.bandwidth==0){
connection.type='none';
}else{
connection.type='unknown';
}
}
varhtml='Type:'+connection.type_text;
html+='<br>Bandwidth:'+connection.bandwidth;
html+='<br>isOnline:'+navigator.onLine;
re_el.innerHTML=html;
}
btn_el.onclick=function(){
re_el.innerHTML='Waiting...';
get_status();
}
</script>
㈤ 怎麼用js腳本判斷自動檢測網路連接是否正常
用AJAX
㈥ 求一段JS代碼,可以根據UA判斷不通的操作系統顯示不同的代碼!
varcheckSystem=function(){
varuaMap={
'android':/Android/i,
'ios':/(?:iPhone|iPad)/i
};
varua=window.navigator.userAgent;
for(variinuaMap){
if(uaMap[i].test(ua)){
returni;
}
}
returnnull;
};
然後直接用
checkSystem()=='ios'
和
checkSystem()=='android'
判斷。
或者直接這樣
document.body.className=checkSystem();
這樣body上就有系統的class了。
控制CSS就能控制如何顯示了。
㈦ 如何用 js 或 php 判斷手機網路環境
JS代碼 http://www..com/ 換成手機頁面地址
<script type="text/javascript">
if ((navigator.userAgent.match(/(iPhone|iPod|Android|ios)/i))) {
location.replace("http://www..com/");
}
</script>
PHP代碼
if(!function_exists('isMobile'))
{
function IsMobile()
{
//如果有HTTP_X_WAP_PROFILE則一定是移動設備
if(isset($_SERVER['HTTP_X_WAP_PROFILE'])) return TRUE;
//如果via信息含有wap則一定是移動設備,部分服務商會屏蔽該信息
if(isset($_SERVER['HTTP_VIA']))
{
//找不到為flase,否則為true
return stristr($_SERVER['HTTP_VIA'], "wap") ? true : false;
}
//判斷手機發送的客戶端標志,兼容性有待提高
if(isset($_SERVER['HTTP_USER_AGENT']))
{
$clientkeywords = array('nokia','sony','ericsson','mot','samsung','htc','sgh','lg','sharp','sie-','philips','panasonic','alcatel','lenovo','iphone','ipod','blackberry','meizu','android','netfront','symbian','ucweb','windowsce','palm','operamini','operamobi','openwave','nexusone','cldc','midp','wap','mobile');
//從HTTP_USER_AGENT中查找手機瀏覽器的關鍵字
if(preg_match('/('.implode('|', $clientkeywords).')/i', strtolower($_SERVER['HTTP_USER_AGENT'])))
{
return TRUE;
}
}
//協議法,因為有可能不準確,放到最後判斷
if(isset($_SERVER['HTTP_ACCEPT']))
{
//如果只支持wml並且不支持html那一定是移動設備
//如果支持wml和html但是wml在html之前則是移動設備
if((strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') !== false) &&
(strpos($_SERVER['HTTP_ACCEPT'], 'text/html') === false ||
(strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') < strpos($_SERVER['HTTP_ACCEPT'], 'text/html'))))
{
return TRUE;
}
}
return FALSE;
}
}
//php 調用
if(IsMobile())
{
header('location:http://www..com/');
}