Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Carlos Ortega
Carlos Ortega
3,092 Points

Disabling a Certain JS script on Mobile Devices

I'm using an animation script on one of my projects but want to disable it on mobile and devices (iPhone, iPad, Android etc.) I'm fairly new to javascript. I found a "Detect Mobile Browsers.js" but have no idea on how to implement it or use it. Can someone please explain?

2 Answers

I use something like this in one of my JS libraries:

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

So, adding that function to your JavaScript file, you could do this:

if (!isMobile()) {
   // do the animation
}
Carlos Ortega
Carlos Ortega
3,092 Points

I will Try this! Thank You so much Dino