如何将移动用户重定向到移动页面


how to redirect mobile user to mobile page

我为现有网页创建了移动网站,并在其中将代码放在移动文件夹中比如www.testing.com/mobile,它运行良好。

但我需要做的是,如果用户从手机访问,它应该重定向到手机页面,而用户从网络访问,它会转到默认页面。

这里我使用:

<meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0" /> 

以适应页面,但url显示为m.tesing.com移动版如何进行

通过浏览器代理或任何其他方式。谢谢

下载这个detectmobile.js并在你的头标签中进行这些更改

 <head>
                <script src="detectmobile.js"></script>
                <script>
                        try {
                                // This is the URL where mobile
                                detectmobile.defaultMobileURL = "http://m.testing.com";
                                detectmobile.process();
                        } catch(e) {
                                // Make sure that in the fault modes
                                // we do not interrupt execution of other Javascript code
                        }
                </script>
        </head>

您可以使用简单的javascript来检测它,而不是使用jquery:

if( /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ) {
 window.location.replace("http://m.tesing.com");
 //or window.location = "http://m.tesing.com";
}

或使用CSS 的其他技术

/* Smartphones ----------- */
@media only screen and (max-width: 760px) {
  #some-element { display: none; }
}

jQuery:

$( document ).ready(function() {        
    if( $('#some-element').css('display')=='none' {
        window.location.replace("http://m.tesing.com");
     //or window.location = "http://m.tesing.com";    
    }
 });

使用此代码检测移动设备,然后重定向到

    <script language="javascript">
      if( /Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.userAgent) ) 
      {
        window.location = "http://m.domain.com";
      }
    </script>

test()方法已用于测试字符串中的匹配,如果找到匹配,则会进行重定向。将此代码添加到主index.php页面的顶部。

在移动index.php上使用此代码重定向回

    <script language="javascript">
      if( !/Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.userAgent) ) 
      {
        window.location = "http://testing.com";
      }
    </script>

至于m.testing.com,您必须创建一个子域。