手机访问网站自动跳转到手机版
方法一:目前很多网站已经出现了手机版的网站,手机网站是和互联网上的网站同步更新的,大多数人访问我们的网站是直接访问电脑上的域名,不可能为了手机网站重新记住一个域名,大家都知道,中国人都是比较懒的,那么遇到这个情况我们如何面对了,其实办法很简单就是在网站头加入一句判断语句,判断是手机访问网站时就自动跳入手机版,如果是电脑访问就显示正常版本的就可以了。
代码如下:
<script src="http://siteapp.baidu.com/static/webappservice/uaredirect.js" type="text/javascript"></script><script type="text/javascript">uaredirect("你的手机版网址");</script>
这个是利用百度APP应用,做为一个跳转。
注:测试可用
方法二:
访问者如果是使用手机浏览器访问我们的站点,会受到一些限制,为此我们需要制作移动版本,并且在站点的首页加上判断,若访问者是用手机或者其它移动设备访问,页面自动跳转至wap页面。若是PC访问则打开正常页面。
当我用手机访问主站时( www.域名.com),则自动跳转到手机版网站去,在手机中输入如下网址: www.域名.com/?mobile则又可以用手机看到PC版网站。通过JS代码来判断。
具体代码如下:
<script type="text/javascript">
if(window.location.toString().indexOf('pref=padindex') != -1){
}else{
if(/AppleWebKit.*Mobile/i.test(navigator.userAgent) || (/MIDP|SymbianOS|NOKIA|SAMSUNG|LG|NEC|TCL|Alcatel|BIRD|DBTEL|Dopod|PHILIPS|HAIER|LENOVO|MOT-|Nokia|SonyEricsson|SIE-|Amoi|ZTE/.test(navigator.userAgent))){
if(window.location.href.indexOf("?mobile")<0){
try{
if(/Android|Windows Phone|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)){
window.location.href="http://m.sihongmarathon.com/index.php";
}else if(/iPad/i.test(navigator.userAgent)){
window.location.href="http://m.sihongmarathon.com/index.php"
}else{
window.location.href="http://m.sihongmarathon.com/index.php"
}
}catch(e){}
}
}
}
</script>
这样就简单实现了手机访问自动跳转到手机版的效果,并且在手机版中也可以通过www.域名.com/?mobile 网址看PC版网站。
如果用的是phpcms V9建站程序,会出现一个小的问题,就是手机访问跳转到m.域名.com 后,默认访问的是 m.域名.com/iddex.html页,我需要他访问到 m.域名.com/index.php页,所以还要加入如下代码。
<script>
try {if( self.location == "http://m.域名.com/" ) {
top.location.href = "http://m.域名.com/index.php";
}
}
catch(e) {}
</script>
经测试,又上两种方法都可行,建议采用第二种方法。 手机平板等移动端适配跳转URL的js代码
<script type="text/javascript">
if(/AppleWebKit.*mobile/i.test(navigator.userAgent) || (/MIDP|SymbianOS|NOKIA|SAMSUNG|LG|NEC|TCL|Alcatel|BIRD|DBTEL|Dopod|PHILIPS|HAIER|LENOVO|MOT-|Nokia|SonyEricsson|SIE-|Amoi|ZTE/.test(navigator.userAgent))){
if(window.location.href.indexOf("?mobile")<0){
try{
if(/Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)){
window.location.href="手机页面";
}else if(/iPad/i.test(navigator.userAgent)){
window.location.href="平板页面";
}else{
window.location.href="其他移动端页面"
}
}catch(e){}
}
}
</script>
腾讯网的适配代码
<script type="text/javascript">
if(window.location.toString().indexOf('pref=padindex') != -1){
}else{
if(/AppleWebKit.*Mobile/i.test(navigator.userAgent) || (/MIDP|SymbianOS|NOKIA|SAMSUNG|LG|NEC|TCL|Alcatel|BIRD|DBTEL|Dopod|PHILIPS|HAIER|LENOVO|MOT-|Nokia|SonyEricsson|SIE-|Amoi|ZTE/.test(navigator.userAgent))){
if(window.location.href.indexOf("?mobile")<0){
try{
if(/Android|Windows Phone|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)){
window.location.href="http://xw.qq.com/index.htm";
}else if(/iPad/i.test(navigator.userAgent)){
window.location.href="http://www.qq.com/pad/"
}else{
window.location.href="http://xw.qq.com/simple/s/index/"
}
}catch(e){}
}
}
}</script>
页:
[1]