How to add a script on mobile version only?

  • 7 September 2016
  • 2 replies
  • 133 views

As title said, How to add a script on mobile version only?

How can i do it?

For some javascript effect, i only want it appear on mobile version.

Can any body know how to set up?

Thank you !


2 replies

Userlevel 7
Badge +3

Hi @Elvis_Liou,

You are going to have to write your scripts to “target” window sizes below 600px which is the current break-point for the mobile view.

Userlevel 6
Badge +1

I would use media detection in:

$( document ).ready(function() {      
    var isMobile = window.matchMedia("only screen and (max-width: 760px)");

    if (isMobile.matches) {
        //Conditional script here
    }
 });

There’s a great thread here.

Reply