如何仅在移动设备上运行脚本

时间:2018-06-20 05:29:47

标签: javascript html wordpress

我只想为屏幕尺寸小于769px的设备运行某个脚本。环境是WordPress。

我已经有

detectWidth函数可查找屏幕的宽度。

理想情况下,我想执行以下操作:

if(detectWidth() < 769)
 then execute this code:
<script>
  (function (d,s,n,id) {
    var js, fjs = d.getElementsByTagName(s)[0];
    if (d.getElementById(id)) return;
    js = d.createElement(s);
    js.id = id;
    js.className = n;
    js.src = "https://example.com;
    fjs.parentNode.insertBefore(js, fjs);
  }(document,'script','p-mcd-sdk','mcd-sdk-jssdk'));
</script>
<pb-mcd embed-id="4f78-b5bb-b3547f1e51c8"></pb-mcd>

仅供参考,这是detectWidth函数

function detectWidth() {
        return window.screen.width || window.innerWidth || window.document.documentElement.clientWidth || Math.min(window.innerWidth, window.document.documentElement.clientWidth) || window.innerWidth || window.document.documentElement.clientWidth || window.document.getElementsByTagName('body')[0].clientWidth;
    }

0 个答案:

没有答案