Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript Conditional Script Loading Reply To: Conditional Script Loading

#208236
mhodges44
Participant

<script>
function isMobileDevice(){
if (/Android|webOS|iPhone|iPad|iPod|BlackBerry|BB|PlayBook|IEMobile|Windows Phone|Kindle|Silk|Opera Mini/i.test(navigator.userAgent)) {
return true;
}
else {
return false;
}
}
</script>

This function will return true if it detects a mobile device, and false otherwise.

Then in script2.js and script3.js simply wrap all of your code that you do not want to execute in:

<script>
if (!isMobileDevice()){
//…..
}
</script>

If you do not want to load the file altogether, here’s a stack answer about this question.
http://stackoverflow.com/a/15521523

Simply conditionally build a script tag in javascript based on the outcome of isMobileDevice() and append it to wherever you put your scripts.