简体   繁体   English

如何在移动设备上禁用一些js脚本

[英]How to disable some js scripts on mobile devices

i read some topics but they are was not helpfully 我读了一些主题,但是对他们没有帮助

i have 4 script like 我有4个脚本

<script src="http://kleaz.com/fsc/three.min.js"></script>
<script src="http://kleaz.com/fsc/Projector.js"></script>
<script src="http://kleaz.com/fsc/CanvasRenderer.js"></script>
<script src="http://kleaz.com/fsc/me.js"></script>

I don't want to load these scripts on mobile devices so if my window width smaller then 780px i don't want my device will load these scripts. 我不想在移动设备上加载这些脚本,所以如果我的窗口宽度小于780px,我不希望我的设备将加载这些脚本。

how it can be possible? 怎么可能呢?

Try your scripts in following code: 请尝试以下代码中的脚本:

function isMobile() {
return /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);
}

if (!isMobile()) {
//place script you don't want to run on mobile here
//TRY YOUR SCRIPT AS FOLLOWING WAY!!!
document.write('<script src="http://kleaz.com/fsc/three.min.js"></script>');
document.write('<script src="http://kleaz.com/fsc/Projector.js"></script>');
document.write('<script src="http://kleaz.com/fsc/CanvasRenderer.js"></script>');
document.write('<script src="http://kleaz.com/fsc/me.js"></script>');
}

Only load the scripts if the screen size is greater than 780px, something long the lines of: 仅当屏幕尺寸大于780px时才加载脚本,该行长于以下行:

if (window.screen.width > 780) {
  document.write( 
    '<script src="http://kleaz.com/fsc/three.min.js"><\/script>' +
    '<script src="http://kleaz.com/fsc/Projector.js"><\/script>' +
    '<script src="http://kleaz.com/fsc/CanvasRenderer.js"><\/script>' +
    '<script src="http://kleaz.com/fsc/me.js"><\/script>'
  )
}

Though you may want to use DOM methods to add the script elements rather than doucment.write . 尽管您可能想使用DOM方法来添加脚本元素,而不是doucment.write

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

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