简体   繁体   English

如何根据屏幕大小加载.js文件?

[英]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) 我们如何使用WordPress themefolder(wp-content / themename / js / file.js)中的媒体查询来加载特定的Javascript文件

Whe would only like to load grid.js when using a tablet or a desktop browser. 他只想在使用平板电脑或台式机浏览器时加载grid.js。

You should load your grid.js asynchronous: 您应该异步加载grid.js:

 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 Javascript异步NachladenTouchgeräteerkennen

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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