1 Star2 Stars3 Stars4 Stars5 Stars (5 votes, average: 5.00 out of 5)

Redirect users to a Mobile Site Javascript

We can direct people to our mobile site using JavaScript. What this following code does is look for a screen size and then redirects the user.

<script type="text/javascript">
if (screen.width <= 699) {
document.location = "mymobilesite.html";

The following code will target iPhone and iPods to your mobile website.

Tip: Click on images to make them larger. If you find anything wrong on this page or need further assistance please comment below or contact me. 🙂
if ((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))) {

Put this link code in your header so that Google knows where your mobile site is located.

<link rel="alternate" href="http://mywebsite/mobile/" media="only screen and (max-width: 640px)">

More detailed redirects in JavaScript

<script type="text/javascript">
        var getStr = window.location.search.substr(1);
        var getArray = getStr.split ("&");
        var get = {};

        for ( var i = 0; i < getArray.length; i++) {
        var tmpArray = getArray[i].split("=");
        get[tmpArray[0]] = tmpArray[1];
       if (get.m == "yes") {
                window.location = "http://m.example.com"
        else if (get.m == "no") {
                window.location = "http://www.example.com";

Redirect to Mobile site using PHP

<?php if(strstr($_SERVER['HTTP_USER_AGENT'],'iPhone') || strstr($_SERVER['HTTP_USER_AGENT'],'iPod'))
  header('Location: http://yoursite.com/iphone');
Redirect users to a Mobile Site Javascript was last modified: December 16th, 2015 by Maximus Mccullough

Subscribe To My YouTube Channel:

Please like, share and subscribe.

Advanced Programming Made Easy

I hope you enjoyed the article. If I was able to help you please consider a tip for the content.

One Time Tip

more tips

Reoccuring Tips


Cool People Share:

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.