jquery - Call external JS file based on "media screen" value -


i'm trying not working:

<html> <head> <script src="js/menu-collapser.js" type="text/javascript" media="media screen , (max-width: 599px)"></script> </head> ... </html> 

//menu-collapser.js :

jquery(document).ready(function($){     $('.main-navigation li ul').hide();     $('.main-navigation li').has('ul').click(function() {         $(this).children().toggle();     }); }); 

do have idea on how in right way? script work if used directly in header tags.

you can't directly using javascript <script> tags. media queries used in linked css files or inline css styles. basic example:

<link rel="stylesheet" media="screen , (min-width: 900px)" href="desktop.css"/> <link rel="stylesheet" media="screen , (min-width: 571px)" href="tablet.css"/> <link rel="stylesheet" media="screen , (max-width: 570px)" href="mobile.css"/> 

or directly in stylesheets:

@media screen , (max-width: 599px) {   #mobile {      display: block;   } } 

however, can use external asset loader/media query library (require.js, modernizr.js, enquire.js , others), in case, i'm setting example using enquire.js, think it's effective , doesn't require jquery default:

full example

1) include enquire.js (available here):

<script type="text/javascript" src="/js/enquire.js"></script> 

2) create load function - load js files:

<script type="text/javascript">  // loads js files in head element     function loadjs(url)     {         // adding script tag head        var head = document.getelementsbytagname('head')[0];        var script = document.createelement('script');        script.type = 'text/javascript';        script.src = url;         // fire loading        head.appendchild(script);     }  </script> 

3) fire enquire.js , listen media query changes (both on-load , on-resize):

<script type="text/javascript">      enquire.register("screen , (max-width: 599px)", {         match : function() {             // load mobile js file             loadjs('mobile.js');         }     }).listen();       enquire.register("screen , (min-width: 600px) , (max-width: 899px)", {         match : function() {             // load tablet js file             loadjs('tablet.js');             //console.log('tablet loaded');         }     }).listen();       enquire.register("screen , (min-width: 900px)", {         match : function() {             // load desktop js file             loadjs('desktop.js');             //console.log('desktop loaded');         }     }).listen(); </script> 


putting together

using simple html page enquire.js loaded external file:

<html> <head>  <script type="text/javascript" src="/enquire.js"></script>  <script type="text/javascript">  // loads js files in head element     function loadjs(url)     {         // adding script tag head        var head = document.getelementsbytagname('head')[0];        var script = document.createelement('script');        script.type = 'text/javascript';        script.src = url;         // fire loading        head.appendchild(script);     }  </script>  <style>      body {       font-family: arial;     }     h1 {       font-size: 50pt;     }      #mobile {       display: none;     }     #tablet {       display: none;     }     #desktop {       display: none;     }      @media screen , (max-width: 599px) {         #mobile {             display: block;         }     }      @media screen , (min-width: 600px) , (max-width: 899px) {         #tablet {             display: block;         }     }      @media screen , (min-width: 900px) {         #desktop {             display: block;         }     }     </style>  </head> <body>      <div id="desktop">         <h1>desktop</h1>     </div>      <div id="tablet">         <h1>tablet</h1>     </div>      <div id="mobile">         <h1>mobile</h1>     </div>      <script type="text/javascript">          enquire.register("screen , (max-width: 599px)", {             match : function() {                 // load js file                 loadjs('mobile.js');             }         }).listen();           enquire.register("screen , (min-width: 600px) , (max-width: 899px)", {             match : function() {                 loadjs('tablet.js');                 //console.log('tablet loaded');             }         }).listen();           enquire.register("screen , (min-width: 900px)", {             match : function() {                 loadjs('desktop.js');                 //console.log('desktop loaded');             }         }).listen();      </script> </body> </html> 

in addition loading js files, create css loader too, work in same way (conditionally), defeats object of using @media in css. it's worth reading usage explanations enquire.js, can lot more i've illustrated here.

caveat: nothing above uses jquery, take advantage of of functions offers; loading scripts example - or executing other functions need to.


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -