简体   繁体   中英

How to load a .js-file depending on screen size?

How can we load a specific Javascript-file using the media queries from the WordPress themefolder (wp-content/themename/js/file.js)

Whe would only like to load grid.js when using a tablet or a desktop browser.

You should load your grid.js asynchronous:

 function is_touch_device() { return !!('ontouchstart' in window); } function loadGridJs() { var scriptTag = document.createElement('script'); scriptTag.src = "//wp-content/themename/js/grid.js"; scriptTag.type = 'text/javascript'; scriptTag.async = true; var headTag = document.getElementsByTagName('head')[0]; headTag.appendChild(scriptTag); } if (is_touch_device()) { loadGridJs(); } 

See:

Javascript asynchron nachladen , Touchgeräte erkennen

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM