Question

I created mobile site for existing webpage and in it, I placed the code in mobile folder like www.testing.com/mobile and it works fine.

But what I need to do is if the user visited from mobile it should be redirected to mobile page and user visited from web it goes to default pages.

Here I used:

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

to fit the page but url shows like m.tesing.com for mobile how to do this

By browser agent or any other ways. Thanks.

Was it helpful?

Solution

Download this detectmobile.js and make this changes in your head tag

 <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>

OTHER TIPS

Instead of using jquery you can use simple javascript to detect it:

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";
}

Or other technique with 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";    
    }
 });

Use this code for detecting mobile devices and then redirect it

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

The test() method has been used to test for a match in a string and if a match is found then a redirection takes place. Add this code at the top of the main index.php page.

Use this code on the mobile index.php to redirect back

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

As for the m.testing.com you will have to create an sub-domain.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top