479b234282ac1eeb452a7eda4989f2f95e0bf264
[platform/framework/web/web-ui-fw.git] / libs / js / jquery-mobile-1.0.1pre / js / jquery.mobile.media.js
1 /*
2 * a workaround for window.matchMedia
3 */
4
5 (function( $, undefined ) {
6
7 var $window = $( window ),
8         $html = $( "html" );
9
10 /* $.mobile.media method: pass a CSS media type or query and get a bool return
11         note: this feature relies on actual media query support for media queries, though types will work most anywhere
12         examples:
13                 $.mobile.media('screen') //>> tests for screen media type
14                 $.mobile.media('screen and (min-width: 480px)') //>> tests for screen media type with window width > 480px
15                 $.mobile.media('@media screen and (-webkit-min-device-pixel-ratio: 2)') //>> tests for webkit 2x pixel ratio (iPhone 4)
16 */
17 $.mobile.media = (function() {
18         // TODO: use window.matchMedia once at least one UA implements it
19         var cache = {},
20                 testDiv = $( "<div id='jquery-mediatest'>" ),
21                 fakeBody = $( "<body>" ).append( testDiv );
22
23         return function( query ) {
24                 if ( !( query in cache ) ) {
25                         var styleBlock = document.createElement( "style" ),
26                                 cssrule = "@media " + query + " { #jquery-mediatest { position:absolute; } }";
27
28                         //must set type for IE!
29                         styleBlock.type = "text/css";
30
31                         if ( styleBlock.styleSheet  ){
32                                 styleBlock.styleSheet.cssText = cssrule;
33                         } else {
34                                 styleBlock.appendChild( document.createTextNode(cssrule) );
35                         }
36
37                         $html.prepend( fakeBody ).prepend( styleBlock );
38                         cache[ query ] = testDiv.css( "position" ) === "absolute";
39                         fakeBody.add( styleBlock ).remove();
40                 }
41                 return cache[ query ];
42         };
43 })();
44
45 })(jQuery);