Adding a Desktop Mobile Toggle Button

The goal of a Bootstrap site is to deliver a mobile view that is so easily navigable that it never leaves the user yearning for a tiny shrunk down desktop experience on their phone. Still, despite a developers best intentions, some users just want what they want. And we should give it to them. We’re not designing sites so that we may enforce our will on others, but to provide content that users find easy to view, and some users have different preferences than others.

Luckily, we can easily provide both worlds, and default to a responsive view with the hopes that they will find it so convincing they’ll never need the desktop view.

Here’s a little snippet of code that I like to put on websites. You can see it in action on: http://kylemitofsky.com/

Add two toggles to your page.

<!-- Desktop / Mobile Footer -->
<div id="DesktopMobileFooter" class='h_center'>
    <hr/>
    <div class="MobileToggle">
        View <a id="DesktopSite" href="#" >Desktop</a> | <strong>Mobile</strong> Site
    </div>
    <div class="MobileToggle" style="display:none;">
        View <strong>Desktop</strong> | <a id="MobileSite" href="#" >Mobile</a> Site
    </div>
</div>

Then add the following JavaScript:

$(".MobileToggle a").click(function () {
    var viewport = ($(this).attr('id') === "MobileSite") ?
                    'width=device-width, initial-scale=1.0' :
                    'width=1200';
    $("meta[name=viewport]").attr('content', viewport);
    $(".MobileToggle").toggle();
    return false;
});

NOTE: As AJ pointed out in the comments, for this solution to work (and Bootstrap in general) make sure you’ve included the tags from Bootstrap’s Basic Template, specifically the viewport meta tag:

<meta name="viewport" content="width=device-width, initial-scale=1">

37 comments:

  1. Hi Kyle, I have tried your solution it changes the view port but the site does not refresh. Could you please provide me the relevant solution for this ? Thanks,Ankit

    ReplyDelete
    Replies
    1. I'm sure this is a couple years too late...but...for anyone else finding this snippet, it'll be a good FYI:

      You need to make sure to add the necessary viewport meta tag, otherwise there's nothing to update the content of:

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

      After adding this (remove the space between "<" and "meta" since it wouldn't let me post it here without a space), it'll work as expected; no refresh needed :)

      Delete
    2. Thanks AJ - yeah, it's part of bootstrap docs and bootstrap isn't really going to work without it in the first place, but doesn't hurt to have the reminder here as the solution is aimed at manipulating it specifically. I'll also add the snippet to the post so google doesn't try to remove angle brackets to prevent html injection.

      Delete
  2. I did however, notice, in case anyone else has had this issue - on the newer Mobile Safari (iPhone 6 and later I guess) I'm seeing that as soon as I try to pinch zoom, it reverts back to the responsive site. I'm guessing it's that the viewport size is being changed when the zoom occurs, but still messing around with it...

    Anyone else having the same issue that has a solution?

    ReplyDelete
    Replies
    1. Ok, fixed it...turns out the version of Bootstrap I was using has a viewport fix that changes the viewport size on gesture. I added a conditional statement with a bool in session storage that allowed me to keep the fix when responsive, but removed when desktop size.

      Thanks again for this great solution!

      Delete
  3. Excellent article. Very interesting to read. I really love to read such a nice article. Thanks! keep rocking. The Sims Mobile Hack

    ReplyDelete
  4. I’m going to read this. I’ll be sure to come back. thanks for sharing. and also This article gives the light in which we can observe the reality. this is very nice one and gives indepth information. thanks for this nice article... unlockmobiledevice

    ReplyDelete
  5. Later head phones made way for Bluetooth, which made things much more simple and easy. Here the task of carrying a head set was reduced as Bluetooth is comparatively small and simple when compare to the head set.How to Activate iPhone

    ReplyDelete
  6. A mobile phone is an exceedingly close to home specialized gadget. Mobile advertising isn't tied in with spamming uninterested individuals with superfluous SMS messages at 2:00 am. Advertisers need to regard this.Google USB Driver

    ReplyDelete
  7. Apps on iPhone provide easy access for consumers and easy tools for advertisers for entrepreneurs iphone giveaway

    ReplyDelete
  8. It is interesting to read your blog post and I am going to share it with my friends.aybabg

    ReplyDelete
  9. When your website or blog goes live for the first time, it is exciting. That is until you realize no one but you and your. Mi mobile price list

    ReplyDelete
  10. Took me time to read all the comments, but I really enjoyed the article. It proved to be Very helpful to me and I am sure to all the commenters here! It’s always nice when you can not only be informed, but also entertained! Best Gaming Laptops Under Rs 60000 for this month

    ReplyDelete
  11. This comment has been removed by the author.

    ReplyDelete
  12. Your blog provided us with valuable information to work with. Each & every tips of your post are awesome. Thanks a lot for sharing. Keep blogging.. video bokep terbaru

    ReplyDelete
  13. In spite of the fact that call cost reserve funds are the simplest to quantify, they give minimal scale to investment funds. Android Development Trends 2019

    ReplyDelete
  14. Thank you because you have been willing to share information with us. we will always appreciate all you have done here because I know you are very concerned with our. Mobile home releveling

    ReplyDelete
  15. Hey what a brilliant post I have come across and believe me I have been searching out for this similar kind of post for past a week and hardly came across this. Thank you very much and will look for more postings from you. aplicativos grĂ¡tis para vender

    ReplyDelete
  16. The only knowledge you will have when you are out to buy a desktop will be the type of desk top you wish to acquire. Click Here

    ReplyDelete
  17. If you are looking for more information about flat rate locksmith Las Vegas check that right away. https://impactresearch.org

    ReplyDelete
  18. You can all the more likely choose that once you comprehend the purchasing procedure an individual experiences before burning through cash on an administration or an item. rtb ad network

    ReplyDelete
  19. This is an excellent post I seen thanks to share it. It is really what I wanted to see hope in future you will continue for sharing such a excellent post. Laptop Repair Dubai

    ReplyDelete
  20. Your work is very good and I appreciate you and hopping for some more informative posts control parental android

    ReplyDelete
  21. Your work is very good and I appreciate you and hopping for some more informative posts control parental android

    ReplyDelete
  22. Desktop PC's are a must in offices, schools, and on homes. We can do a lot of wonderful and worthy things with desktop PC's.Best 4k 240hz monitors 2019

    ReplyDelete
  23. If you need some more help so you can visit my profile Laptop Repair Dubai.

    ReplyDelete
  24. The following 4 are types of mobile technology that automakers and GSMA expect to see built into all vehicles during the coming few years. Rising Thunder

    ReplyDelete
  25. Here are some ways by which mobile technology can be used to run a hospitality service more efficiently. Best N64 Games

    ReplyDelete
  26. Courses on mobile technology have become a professional field of study. It covers everything about a "mobile phone" or rather we should say a "smart phone." RCA Universal Remote

    ReplyDelete
  27. This comment has been removed by the author.

    ReplyDelete
  28. It is extremely important to new model number printers with www-canoncomijsetup.com.
    This can be done with the help of effective printes all devices used on digital platforms
    Read more:
    canon.com/ijsetup

    ReplyDelete
  29. Great Article I was looking for information about this topic from a long time. Satisfy with the content of the blog.
    hulu.com/activate

    ReplyDelete
  30. I like your post. It is good to see you verbalize from the heart and clarity on this important subject can be easily observed... android flasher

    ReplyDelete
  31. Catch Runaways, The First, Harlots, Casual and much more interesting shows on Hulu. Not just shows, watch hit movies across all genres by activating Hulu on your device. Get the app and follow the www.hulu.com/activate procedures. Within few minutes, you are ready to stream hundreds of shows and movies.

    Visit:
    www.hulu.com/activate
    hulu.com/activate
    Hulu activate
    www.hulu.com/account
    www.hulu.com/activate activation code
    www.hulu.com/activate code
    Hulu activation

    ReplyDelete