A
A
angelzzz2016-01-14 10:23:22
JavaScript
angelzzz, 2016-01-14 10:23:22

How to disable js execution under certain conditions?

I have js code

<script type="text/javascript">
        $(document).ready(function() {
            var v = $("#sticknavbar");
            var z = $("#sticknavbottomid")
            var stickyHeaderTop = v.offset().top;
            $(window).scroll(function() {            
                if ( $(window).scrollTop() >= stickyHeaderTop) {
        			v.addClass("sticknav"); //добавить класс sticknav
        			z.addClass("sticknavbottom");
               } else {
        			v.removeClass(); //при достижении нижней части убрать sticknav        		
        			z.removeClass("sticknavbottom");
               }
              });
        });    
    </script>


1. How to disable code execution if the screen resolution is less than 992px?
2. How to set up a check for the presence of #sticknavbar or #sticknavbottomid (there is another code with more id), and if they are not there, then do not execute the code. Otherwise, now in the absence of these id, no other code is executed.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
seriogja, 2016-01-15
@seriogja

1) Set the window resize event to save the size of this window. Check (when resizing) whether the width is larger than necessary.
2)

if ( $( "#myDiv" ).length ) {
    // элемент есть
    $( "#myDiv" ).show();
}

And by the way, it's absolutely not clear how the code you posted in the post is related to the question. How it can help, and why you posted it - it's not clear.

A
angelzzz, 2016-01-18
@angelzzz

I'll answer myself. I solved the issue like this:

<script type="text/javascript">
        var isMobile = {
            Android:        function() { return navigator.userAgent.match(/Android/i) ? true : false; },
            BlackBerry:     function() { return navigator.userAgent.match(/BlackBerry/i) ? true : false; },
            iOS:            function() { return navigator.userAgent.match(/iPhone|iPad|iPod/i) ? true : false; },
            Windows:        function() { return navigator.userAgent.match(/IEMobile/i) ? true : false; },
            any:            function() { return (isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Windows());  }
        };
      
        if ( !isMobile.any() ) {
            $(document).ready(function() {
                var v = $("#sticknavbar");
                var z = $("#sticknavbottomid")
                var stickyHeaderTop = v.offset().top;
            
                $(window).scroll(function() {
                	//var windowpostest = $(window).scrollTop();
                	//v.html("расстояния от верха" + "<br />"+ stickyHeaderTop + "<br /> mestopozhenie " + windowpostest + "<br />"+ v.offset().top
                    //);
                
                    if ( $(window).scrollTop() >= stickyHeaderTop) {
            			v.addClass("sticknav"); //добавить класс sticknav
            			z.addClass("sticknavbottom");
                   } else {
            			v.removeClass(); //при достижении нижней части убрать sticknav        		
            			z.removeClass("sticknavbottom");
                   }
                  });
            });    
        }
    </script>

Where does this construct disable the display of js on mobile
var isMobile = {
            Android:        function() { return navigator.userAgent.match(/Android/i) ? true : false; },
            BlackBerry:     function() { return navigator.userAgent.match(/BlackBerry/i) ? true : false; },
            iOS:            function() { return navigator.userAgent.match(/iPhone|iPad|iPod/i) ? true : false; },
            Windows:        function() { return navigator.userAgent.match(/IEMobile/i) ? true : false; },
            any:            function() { return (isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Windows());  }
        };
      
        if ( !isMobile.any() ) {
        }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question