Thursday, June 9, 2016

How to know which device you are viewing using Bootstrap and JQuery function

Bootstrap automatically detect which device size you are viewing a web page

Bootstrap detect four device sizes
xs - Extra Small
sm - Small
md - Medium
lg - Large


When we develop websites or web applications sometime there may be a situation when you need to identify which device size it is, below is the JQuery code to identify which device size.


<script type="text/javascript">
    function findBootstrapEnvironment() {
        var envs = ['xs', 'sm', 'md', 'lg'];

        var $el = $('<div>');
        $el.appendTo($('body'));

        for (var i = envs.length - 1; i >= 0; i--) {
            var env = envs[i];

            $el.addClass('hidden-' + env);
            if ($el.is(':hidden')) {
                $el.remove();
                return env;
            }
        }
    }
    function fnLeftFilterHideShow() {
        if (findBootstrapEnvironment() == "xs") {
            $('#abc').collapse('hide');
        } else {
            $('#abc').collapse('show');
        }
    }
</script>

No comments:

Post a Comment