Skip to content

Instantly share code, notes, and snippets.

@markthelefty
Created June 4, 2013 01:23
Show Gist options
  • Save markthelefty/5702910 to your computer and use it in GitHub Desktop.
Save markthelefty/5702910 to your computer and use it in GitHub Desktop.
This is a simple example of using Enquire.js to load AJAX content based on screen width (media queries). In the below example 480.html is loaded via AJAX when the browser is 0px to 959px. From 960px and up 960.html is loaded. They are switched when the browser window shrinks.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Enquire Test</title>
<meta name="viewport" content="width=device-width">
<!-- Normalize CSS -->
<link rel="stylesheet" src="http://normalize-css.googlecode.com/svn/trunk/normalize.css"/>
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/css/bootstrap-combined.min.css"/>
</head>
<body>
<div id="result-1">
</div>
<div id="result-2">
</div>
<!-- jQuery -->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<!-- Enquire.js -->
<script src="http://cdnjs.cloudflare.com/ajax/libs/enquire.js/2.0.0/enquire.min.js"></script>
<script>
(function () {
var result1 = $('#result-1')
var result2 = $('#result-2')
enquire.register("screen and (max-width: 959px)", {
deferSetup: true,
setup: function () {result1.load('ajax/480.html');},
match: function () {result1.fadeIn();},
unmatch: function () {result1.hide();}
});
enquire.register("screen and (min-width: 960px)", {
deferSetup: true,
setup: function () {result2.load('ajax/960.html');},
match: function () {result2.fadeIn();},
unmatch: function () {result2.hide();}
});
})();
</script>
</body>
</html>
@markthelefty
Copy link
Author

Would love to hear any improvements.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment