Monday, January 26, 2015

[Javascript] The best way for to catch is mobile browser!

Are you web programmer? If so, when you make mobile web site you want to know what is mobile browser. There is many way, but here is simple & best way with javascript.


First best way!

Make isMobile function. Add it to your js file or section.
function isMobile() {
   return /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);
}

[Code testing in desktop browser - return false]



Second best way with css.

Step 01. In your css section or file, add it.
@media only screen and (max-width: 760px) { #mobile_flag { display: none; } }

* you can change screen size 760 to some other size.

Step 02. In your html file add it.
<div id="mobile_flag"></div>

Step 03. In your js file or section, add it.
function isMobile() {
   if( $('#mobile_flag').css('display')=='none') {
        return true;       
   }
   return false;
}




No comments:

Post a Comment