What is enquire.js?
enquire.js is a lightweight, pure JavaScript library for handling media queries in a responsive design. It allows developers to register and unregister callbacks for media query changes, making it easier to manage responsive behaviors in web applications.
What are enquire.js's main functionalities?
Registering a Media Query
This feature allows you to register a media query and define callbacks for when the media query is matched or unmatched. In this example, a message is logged to the console when the screen width is less than or equal to 600px and another message when it is greater.
enquire.register("(max-width: 600px)", {
match: function() {
console.log("Screen width is less than or equal to 600px");
},
unmatch: function() {
console.log("Screen width is greater than 600px");
}
});
Unregistering a Media Query
This feature allows you to unregister a previously registered media query. In this example, the media query for a screen width of 600px is first registered and then unregistered.
var handler = {
match: function() {
console.log("Screen width is less than or equal to 600px");
},
unmatch: function() {
console.log("Screen width is greater than 600px");
}
};
enquire.register("(max-width: 600px)", handler);
enquire.unregister("(max-width: 600px)", handler);
Using Multiple Handlers
This feature allows you to register multiple handlers for a single media query. In this example, two handlers are registered for the same media query, each logging different messages to the console.
enquire.register("(max-width: 600px)", [
{
match: function() {
console.log("Handler 1: Screen width is less than or equal to 600px");
},
unmatch: function() {
console.log("Handler 1: Screen width is greater than 600px");
}
},
{
match: function() {
console.log("Handler 2: Screen width is less than or equal to 600px");
},
unmatch: function() {
console.log("Handler 2: Screen width is greater than 600px");
}
}
]);
Other packages similar to enquire.js
matchmedia-polyfill
matchmedia-polyfill is a polyfill for the window.matchMedia method, which allows you to test media queries in JavaScript. Unlike enquire.js, it does not provide a way to register callbacks for media query changes, but it can be used to check if a media query matches the current state.
mediaquery
mediaquery is a small library for handling CSS media queries in JavaScript. It provides a simple API for adding and removing listeners for media query changes. Compared to enquire.js, it offers a more minimalistic approach with fewer features but can be sufficient for basic use cases.
css-mediaquery
css-mediaquery is a library for parsing and evaluating CSS media queries in JavaScript. It allows you to check if a media query matches a given set of conditions. Unlike enquire.js, it does not provide a way to register callbacks for media query changes, but it can be useful for evaluating media queries programmatically.
enquire.js - Awesome Media Queries in JavaScript

enquire.js
is a lightweight, pure javascript library (with no dependencies) for programmatically responding to media queries.
Getting enquire.js
Download
Get the latest build, ready to go:
Install via Bower
To install via bower, enter the following at the command line:
bower install enquire
Install via npm
To install via npm, enter the following at the command line:
npm install enquire.js
Build From Source
If you want build from source (and run all unit tests etc):
git clone git://github.com/WickyNilliams/enquire.js.git
cd enquire.js
npm install
grunt
Booya!
Quick Start
The main method you will be dealing with is register
. It's basic signature is as follows:
enquire.register(query , handler );
query
is the CSS media query you wish to respond to, and handler
is an object containing any logic to handle the query. An example of usage is as follows:
enquire.register("screen and (max-width:1000px)", {
match : function() {},
unmatch : function() {},
setup : function() {},
destroy : function() {},
deferSetup : true
});
This should be enough to get you going, but please read the full enquire.js documentation if you wish to learn about the other cool features.
Contributing
- Got an awesome idea?
- Found a not-so-awesome bug?
- Wish to get my attention through an inappropriate communication channel?!
Then please don't hesitate to raise an issue, they will all be looked at and tended to.
And for all the cool cats who are prepared to give their time to contribute code, feel free to open a pull request. If you could write unit tests to accompany your pull request that would be pretty sweet, but no worries if not - if it's good enough to be merged in, it's good enough for me to spend a little time to write tests on your behalf :-)
License
License: MIT (http://www.opensource.org/licenses/mit-license.php)