1

i read some topics but they are was not helpfully

i have 4 script like

<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.

how it can be possible?

3
  • You can load scripts asynchronously...Search about it.. Loading them with conditions will help! 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. 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. Commented Apr 18, 2016 at 8:30

2 Answers 2

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.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.

Sign up to request clarification or add additional context in comments.

Comments

1

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>');
}

2 Comments

I tired this <script type="text/javascript"> function isMobile() { return /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent); } if (!isMobile()) { <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> } </script> but that time page not load my scripts on desktop too
Are you sure that every iPad has a screen size less than 780px? And every IEMobile device? And that every device that includes any one of those words in the Navigator string is actually one of those devices?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.