2014-10-30 3 views
2

다음은 화면 크기에 따라 모바일 디스플레이를 검색하기위한 제 발췌입니다. URL에 forceDesktop 매개 변수를 추가하여 사이트가 데스크톱 모드로 유지되도록 할 수 있습니다.모바일 장치를 감지하고 리디렉션하는 가장 좋은 방법

jquery가 새로워졌습니다. 의견이 있으시면 의견을 말하십시오.

크레딧 brandonjp로 이동 : How can I get query string values in JavaScript?

 <script> 
      $.urlParam = function(name, url) { 
       if (!url) { 
        url = window.location.href; 
       } 
       var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(url); 
       if (!results) { 
        return undefined; 
       } 
       return results[1] || undefined; 
      } 
      window.onload = function() { 
       var forceDesktop = $.urlParam('forceDesktop'); 
       if (!forceDesktop) { 
        if ($(window).width() < 639) { 
         var url = "http://m.mysite.com/";  
         $(location).attr('href',url); 
        } 
       } 
      }; 
     </script> 
+1

나는이 게시물은 2 년 이상 오래 알고 있지만 정말 [코드 검토]에 있어야합니다 (http://codereview.stackexchange.com) 대신에 여기 ... – Ortund

답변

0

는 사실, 나는 윈도우 폭에서 이동을 감지하는 것이 중요하다고 생각 이상

if(/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent))  
{ 
    var url = "http://m.mysite.com/";  
    $(location).attr('href',url); 

} 

입니다.

그래서 여기에 제가 사용하는 방법이 있습니다. 당신은 어떤 형태의 사용하려고하는 경우

function detectmob() { 
    if(window.innerWidth <= 800 || window.innerHeight <= 600) { 
    return true; 
    } else { 
    return false; 
    } 
} 

if (detectmob()){ 
top.location.href="mobile"; 
} 
+0

많은 휴대 전화 화면이 너비가 720px 또는 800px이고 너비가 480px이기 때문에 'AND'가 아니라'OR '이 필요하다고 생각합니다. – pawel

+0

네가 맞다면 대답을 편집 할 수있다. –

2

가장 좋은 방법은 Here

+0

왜 jQuery를뿐 아니라'위치 .replace ("http://m.mysite.com/")'? – pawel

+0

네, 그것도 유효합니다.'location.replace ("http://m.mysite.com/")'그러나 그는 다른 목적으로 URL을 사용하고자한다면'var'에서 캐시 할 수 있습니다. –

1

Modernizr 같은 것을 통해 특징 검출보다는 브라우저를 스니핑, 가장 좋은 건 http://detectmobilebrowsers.com/에서 사용자가 생산/불완전 스크립트를 여기에 붙여 넣기보다는 약간의 스크립트를 잡아 것입니다 거기.

2

왜 안 되니?

if(/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)) { 
    window.location = "http://m.mysite.tld/"; 
} 
관련 문제