Skip to content

Instantly share code, notes, and snippets.

@joyrexus
Created November 4, 2013 19:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joyrexus/7307525 to your computer and use it in GitHub Desktop.
Save joyrexus/7307525 to your computer and use it in GitHub Desktop.
Minimal jquery sans jquery

Quick demo how you can package up the vanilla-js equivalents of jquery methods to emulate jquery.


Avoid using jquery when you just need to select elements or make a simple ajax request.

See this gist for an overview of vanilla-js equivalents of additional jquery methods.

<!DOCTYPE html>
<meta charset="utf-8">
<script src="http://jashkenas.github.io/coffee-script/extras/coffee-script.js"></script>
<script src="sans-jq.js"></script>
<style>
#file {
margin-top: 25px;
margin-left: 50px;
font-family: "Helvetica Neue", sans-serif;
font-weight: 100;
font-size: 75px;
color: #777;
-webkit-font-smoothing: antialiased;
}
</style>
<body>
<div id="file"></div>
<img id="image" src=""/>
<script type="text/coffeescript">
url = 'https://api.github.com/gists/7280524'
insert = (res) ->
gist = JSON.parse res.target.responseText
thumb = gist.files['thumbnail.png.B64']
$('#file').textContent = thumb.filename
$('#image').src = "data:image/png;base64," + thumb.content
$.get url, insert
</script>
root = exports ? @
$ = (selector) ->
if selector.match /^#/
document.querySelector selector
else
document.querySelectorAll selector
$.get = (url, callback) ->
xhr = new XMLHttpRequest()
xhr.open "GET", url, true
xhr.onload = callback
xhr.send()
root['$'] = $
// Generated by CoffeeScript 1.6.3
(function() {
var $, root;
root = typeof exports !== "undefined" && exports !== null ? exports : this;
$ = function(selector) {
if (selector.match(/^#/)) {
return document.querySelector(selector);
} else {
return document.querySelectorAll(selector);
}
};
$.get = function(url, callback) {
var xhr;
xhr = new XMLHttpRequest();
xhr.open("GET", url, true);
xhr.onload = callback;
return xhr.send();
};
root['$'] = $;
}).call(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment