HTM静态网页手机端跳转pc 或者pc跳转手机端JS代码

◇◆丶佛笑我妖孽 提交于 2019-12-12 08:57:10

【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>>

<script type="text/javascript">    
var siteurl = 'https://m.yourdomain.com';    
// 跳桌面版    
if (!(navigator.userAgent.match(/(iPhone|iPod|Android|Windows Phone)/i))) {    
var url=window.location.href;    
// url=url.replace('http://m.','http://www.');    
url=url.replace('https://m.','https://www.');    
location.replace(url);    
}    
</script>    

上面代码是在手机模板head内加入

<script type="text/javascript">
            var new_url = window.location.href;
            if(new_url.indexOf('http://yourdomain') >= 0 || new_url.indexOf('https://yourdomain') >= 0) {
                new_url = new_url.replace('http://yourdomain', 'http://www.yourdomain');
                new_url = new_url.replace('https://yourdomain', 'http://www.yourdomain');
                window.location.replace(new_url);
            }
            var siteurl = 'http://www.iyourdomain.net/';
        </script>
                <script type="text/javascript">
            <!--跳m站-->
            function getCookie(nm) {
                var arr, reg = new RegExp("(^| )" + nm + "=([^;]*)(;|$)");
                if (arr = document.cookie.match(reg)) {
                    return unescape(arr[2]);
                } else {
                    return null;
                }
            }
            if ((navigator.userAgent.match(/(iPhone|iPod|Android|Windows Phone)/i))) {
                if(getCookie('mfjian') == null) {
                    var url = window.location.href;
                    url = url.replace('http://yourdomain', 'http://m.yourdomain');
                    url = url.replace('https://yourdomain', 'https://m.yourdomain');
                    url = url.replace('http://www.', 'http://m.');
                    url = url.replace('https://www.', 'https://m.');
                    location.replace(url);
                }
            }/*else{
                var curProtocol = window.location.protocol.split(':')[0];
                if (curProtocol === 'http') {
                    var url=window.location.href;
                    url=url.replace('http://','https://');
                    location.replace(url);
                }
            }*/
        </script>
        
        
         在电脑模板head中加入

虽然有很多cms在后台PHP端已经做了客户访问端的平台属性判断,但是有很多时候 ,页面被缓存成静态,是不能跳转的,要加js识别代码。

故而,以上代码变得尤其重要。

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!