js判断手机访问或者PC的几个例子(常用于手机跳转)
作者:bea
例子一 <script>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 = sUser
例子一
<script>
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='http://';
}else{
//window.location.href='http://m.';
}
}
$(function(){
browserRedirect();
});
</script>
例子2
<script type="text/javascript">
(function(){
var reWriteUrl = function(url){
if(url){
var Splits = url.split("/"),siteName=window.location.pathname;
if(typeof siteName!=="undefined"){
return "http://m."+siteName;
}
}
};
if(/Android|webOS|iPhone|iPad|Windows Phone|iPod|BlackBerry|SymbianOS|Nokia|Mobile/i.test(navigator.userAgent)){
var url=window.location.href;
var pathname=window.location.pathname;
if(url.indexOf("?mobile")<0){
try{
window.location.href=reWriteUrl(url);
}catch(e){}
}
};
})();
</script>
注意,如果给搜索引擎看的话我们在文件头加入 <meta http-equiv="mobile-agent" content="format=xhtml; url=http://m." /> <meta http-equiv="mobile-agent" content="format=html5; url=http://m." /> 意思是告诉搜索引擎移动蜘蛛抓取的是手机版本的.
有用 | 无用
<script>
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='http://';
}else{
//window.location.href='http://m.';
}
}
$(function(){
browserRedirect();
});
</script>
例子2
<script type="text/javascript">
(function(){
var reWriteUrl = function(url){
if(url){
var Splits = url.split("/"),siteName=window.location.pathname;
if(typeof siteName!=="undefined"){
return "http://m."+siteName;
}
}
};
if(/Android|webOS|iPhone|iPad|Windows Phone|iPod|BlackBerry|SymbianOS|Nokia|Mobile/i.test(navigator.userAgent)){
var url=window.location.href;
var pathname=window.location.pathname;
if(url.indexOf("?mobile")<0){
try{
window.location.href=reWriteUrl(url);
}catch(e){}
}
};
})();
</script>
注意,如果给搜索引擎看的话我们在文件头加入 <meta http-equiv="mobile-agent" content="format=xhtml; url=http://m." /> <meta http-equiv="mobile-agent" content="format=html5; url=http://m." /> 意思是告诉搜索引擎移动蜘蛛抓取的是手机版本的.
有用 | 无用
猜你喜欢
您可能感兴趣的文章:
- JS实现合并两个数组并去除重复项只留一个的方法
- JS数组合并push与concat区别分析
- 理解Javascript的call、apply
- javascript实现网站加入收藏功能
- javascript实现无缝上下滚动特效
- js实现将选中内容分享到新浪或腾讯微博
- JavaScript运行过程中的“预编译阶段”和“执行阶段”
- Sublime Text 3常用插件及安装方法
- 基于MVC4+EasyUI的Web开发框架形成之旅之界面控件的使用
- 基于JavaScript获取鼠标位置的各种方法
- javascript数组克隆简单实现方法
- Perl Substr()函数及函数的应用
- javascript针对不确定函数的执行方法
- 深入浅析JavaScript字符串操作方法 slice、substr、substring及其IE兼容性
- js如何判断输入字符串长度
- 常常会用到的截取字符串substr()、substring()、slice()方法详解
- jQuery添加和删除指定标签的方法
- 详解JavaScript基于面向对象之继承实例
- javascript实现拖放效果