How To Redirect Your Website To Mobile Website On Mobile Devices By Javascript

Posted by Ankur Gandhi in Javascript



iphone2

Hello Friends, Some days ago I was working on one project in which I have to redirect site to its mobile website when it opens in iPhone,iPad,iPod,Blackberry and Android by Javascript.I have searched so many time on internet and lastly found one code which was not working perfectly for all devices. So I have changed some things in that code to make it work for iPhone,iPad,iPod,Blackberry and Android. So i though it to post it on my website, So it would be helpful for my users to make them site redirect to iPhone,iPad,iPod,Blackberry and Android.

So below is Javascript code by which you can redirect your website to mobile website on Mobile Devices.

<script language=javascript>
<!--
if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i)) || (navigator.userAgent.match(/blackberry/gi)) || (navigator.userAgent.match(/android/gi)))
{
   location.replace("mobile-page.html");
}
-->
</script>

Put this code at top of your website page, So whenever anyone open your site on mobile device it will redirect to mobile-page.html or you can change it to your mobile site address. Let me know if you have any other great code for mobile redirects.

You can follow any responses to this entry through the RSS 2.0 You can leave a response, or trackback.

  • mjc

    I was having problems getting JS redirect to work with Blackberry, this solved it – thanks!

    • http://www.hackingethics.com/ Ankur Gandhi

      I am glad that it helped you.

      • pankaj

        How about, if we need to open a main website in mobile. I have a Magento website which have Mobile version as well. But i have link in Mobile site footer ” View Main Site”. I want to open main website when user click on that link.

        Quick help is highly appreciated.

        • Cristiano

          Hi did you find the solution?

  • http://twitter.com/iBootLogos iBootLogos

    Will be great for redirecting to the iPhone version of my site

  • Anonymous

    what about for windows phone 7 etc?

    • Luke Davis

      Even though I am insanely late, I would recommend making it send you to an alternate index.html like… erm.. fullindex.html and do that for the rest of em ;) Hope I helped.

  • http://kelvincheong.com Kelvin

    what’s the code for windows phone?

    • Ke

      yes that would be really handy to know, also palm and symbian please?
      thank you very much

  • Amit7950

    Thanks a lot :)

  • Designing Studios

    Hi thanks for the script. But i have a trouble here. I am able to redirect the user to mobile site (where very less information we are showing). We would like to add a new link in the footer called View Full Site. Even though he clicks on it he is redirecting to mobile because the script we added is in the same page i.e. index.html

    Please suggest !!!

  • chillystick

    @greg5green @hackingethics I see what you did there

  • http://www.facebook.com/katcrane1 Katherine Crane

    You Rock!!!! Thanks for the code.Saved me hours figuring this out and made my client very happy.

  • Ahmed Espania

    Thnxxxx xxxxxx

  • http://www.adsyou.com/ سيارات للبيع في الاردن

    I have a Magento website which have Mobile version as well. But i have link in Mobile site footer ” View Main Site”.

  • Nockimey

    YOU ARE SO AWESOME!!!!!

  • Nockimey

    This code is working for the Ipad, but when I try to look up my site with an android phone, it won’t redirect. Is there anything that needs to be changed?

    • Nockimey

      NM, I just reloaded the code and it works just fine. Thanks!

  • http://www.facebook.com/people/Carlos-Correia/100001015209619 Carlos Correia

    and for windows mobile?

  • http://www.nghienphim.net/ Leo Nguyen

    How’s about older phone, not recent smart phone?

  • rabiul

    I have add a link to view full version of site from iPhone version of sit…of your code,which is awesome by the way!How can i make it view the full site?
    Quick help in this problem

  • http://www.ttnc.co.uk/ Business Telephone Numbers

    Great information you have shred with us.I would like to read your more updates.Keep them coming.

  • http://www.facebook.com/profile.php?id=1159343850 Sacha Cohen Scali

    Hi, I would like to know where I have to put the code on my website ; I am working on Joomla! framework. Can anyone help me please ?

  • swat

    How do i fix the code? Do i have to create a subdomain before i put the code?

  • Rene

    Every body that have a full site link and want to redirect to it: Create a new index page and rename it to index1.html but without this code. Link the full site to this page and from there visitors will browse to other pages. Make sure you point all pages from index1.html

    But im having a question for code developer. It works on all android phones and on tablet chrome browser. But when i open paige in adroid browser the code doesn’t redirect.

    Any suggestions? Thanks

  • Tokimon

    Why not measure the screen width instead of sniffing the User Agent String? That is a much more secure way, as you don’t just taget specific brands, but you also target divices that would benefit from having the “mobile” view.