0

I've got a pretty basic jquery function that accepts two arguments, maxWidth and minWidth. The arguments check the users browser size and performs an action based upon their browser size.

$.fn.browserWidth = function(maxWidth, minWidth) {
  maxWidth = maxWidth || 1100;    
  return document.documentElement.clientWidth >= maxWidth;
  return document.documentElement.clientWidth <= minWidth; 
};
})(jQuery);

The first conditional works at a normal browser size:

if(browserWidth(1099)){ //do something }

However the second conditional always fires but I only want it to fire at the mobile breakpoint

if(browserWidth(0,1099)){ //do something }

Am I wrong in setting the first argument to zero? Ideally I don't even want the first argument and just want to check the minWidth in this situation. But there are other instances when I just want to check the maxWidth? Any ideas? Thanks!

4
  • 3
    your second return will never be reached as you have already returned something above. I think you want: return document.documentElement.clientWidth >= maxWidth && document.documentElement.clientWidth <= minWidth; Commented Nov 18, 2013 at 20:20
  • Newb question but how would I return both arguments? Can you please include a code sample, newb here. Commented Nov 18, 2013 at 20:22
  • just edited comment above Commented Nov 18, 2013 at 20:22
  • Why is jQuery even included here? This is a JavaScript question without any jQuery code. Commented Nov 18, 2013 at 20:31

1 Answer 1

1

You may want to accept an object instead:

$.browserWidth = function(options) {
  // Returns true if no options are passed
  var result = true;
  var width  = $(window).width();

  if('lowerThan' in options) {
    result = result && (width <= options.lowerThan);
  }

  if('biggerThan' in options) {
    result = result && (width >= options.biggerThan);
  }

  return result;
};

Usage:

if($.browserWidth({ lowerThan: 1099 }) { // ...

if($.browserWidth({ biggerThan: 1099 }) { // ...

if($.browserWidth({ lowerThan: 2000, biggerThan: 1000 }) { // ...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.