Steve Kim
Steve Kim

Reputation: 5591

jQuery based on mobile or desktop

So I have following js:

 jQuery('.rhmps_aj').click(function(e) {
    ...
    success: function(data){
        //Desktop
        jQuery('.desktop_less').hide();
        jQuery('.desktop_more').show();
        //Mobile
        jQuery('.mobile_less').hide();
        jQuery('.mobile_more').show();

 });

Can someone tell me how I can "disable" certain functions based on whether the content is being viewed via desktop or mobile?

For example, with desktop, I want to disable "mobile" function and vice versa for mobile view.

Upvotes: 1

Views: 4318

Answers (2)

B-hawk
B-hawk

Reputation: 35

I think screen.width might do the trick.

$<!DOCTYPE html>
<html>

<head>
  <script data-require="[email protected]" data-semver="2.1.4" src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
</head>

<body>
  <script>
    var width = screen.width;
    $(document).ready(function() {
      if (width < 600)
        runMobile();
      else
        runDesktop();
    });

    var runMobile = function() {
      alert("Mobile");
    }

    var runDesktop = function() {
      alert("Desktop");
    }
  </script>
</body>

</html>

Upvotes: 0

Anthony
Anthony

Reputation: 1581

You can check the width of the screen size. If the screen size is less than that width you can perform the function you want.

Example:

if ($(window).width() < 600) {       // if width is less than 600px
   MobileFunctions();                 // execute mobile function
}
else {                              // if width is more than 600px
   DesktopFunctions();               // execute desktop function
}

Upvotes: 1

Related Questions