㈠ 使用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/');
}