admin管理员组

文章数量:1410717

i read some topics but they are was not helpfully

i have 4 script like

<script src=".min.js"></script>
<script src=".js"></script>
<script src=".js"></script>
<script src=".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.

how it can be possible?

i read some topics but they are was not helpfully

i have 4 script like

<script src="http://kleaz./fsc/three.min.js"></script>
<script src="http://kleaz./fsc/Projector.js"></script>
<script src="http://kleaz./fsc/CanvasRenderer.js"></script>
<script src="http://kleaz./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.

how it can be possible?

Share Improve this question asked Apr 18, 2016 at 8:23 Cihan ZenginCihan Zengin 1032 silver badges12 bronze badges 3
  • You can load scripts asynchronously...Search about it.. Loading them with conditions will help! – Rayon Commented Apr 18, 2016 at 8:24
  • Aka, turn your logic around. Only load those script when the windows bigger than 780. You can dynamically add scripts by making a new script tag and inserting it into the body after setting its src. – Shilly Commented Apr 18, 2016 at 8:25
  • Surely this is a duplicate. Simply detect screen width, and if 780 or less, don't load the scripts. – RobG Commented Apr 18, 2016 at 8:30
Add a ment  | 

2 Answers 2

Reset to default 4

Only load the scripts if the screen size is greater than 780px, something long the lines of:

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

Though you may want to use DOM methods to add the script elements rather than doucment.write.

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./fsc/three.min.js"></script>');
document.write('<script src="http://kleaz./fsc/Projector.js"></script>');
document.write('<script src="http://kleaz./fsc/CanvasRenderer.js"></script>');
document.write('<script src="http://kleaz./fsc/me.js"></script>');
}

本文标签: javascriptHow to disable some js scripts on mobile devicesStack Overflow