Skip to content

Instantly share code, notes, and snippets.

View icodejs's full-sized avatar

Tahir Joseph icodejs

View GitHub Profile
@icodejs
icodejs / imageloading.js
Created April 27, 2012 15:50 — forked from khalillechelt/imageloading.js
JavaScript: Image Loading by Remy Sharp
// when the DOM is ready
$(function () {
var img = new Image();
// wrap our new image in jQuery, then:
$(img)
// once the image has loaded, execute this code
.load(function () {
// set the image hidden by default
$(this).hide();
@icodejs
icodejs / geoloco.js
Created May 9, 2012 12:47
Geo location testing using W3C Geolocation API and Google maps API
$(function () {
$('#get_ip').on('click', function (e) {
e.preventDefault();
$.ajax({
type: 'GET',
url: 'http://api.hostip.info/get_json.php',
dataType: 'json',
success: function (data, status) {
console.log(data);
@icodejs
icodejs / pubsub.md
Created May 17, 2012 14:08 — forked from addyosmani/pubsub.md
Four ways to do Pub/Sub with jQuery 1.7 and jQuery UI (in the future)

#Four Ways To Do Pub/Sub With jQuery 1.7 and jQuery UI (in the future)

Between jQuery 1.7 and some of work going into future versions of jQuery UI, there are a ton of hot new ways for you to get your publish/subscribe on. Here are just four of them, three of which are new.

(PS: If you're unfamiliar with pub/sub, read the guide to it that Julian Aubourg and I wrote here http://msdn.microsoft.com/en-us/scriptjunkie/hh201955.aspx)

##Option 1: Using jQuery 1.7's $.Callbacks() feature:

$.Callbacks are a multi-purpose callbacks list object which can be used as a base layer to build new functionality including simple publish/subscribe systems. We haven't yet released the API documentation for this feature just yet, but for more information on it (including lots of examples), see my post on $.Callbacks() here:

@icodejs
icodejs / eventObject.js
Created May 17, 2012 15:19
Catching keyboard events
$(function() {
$('body').keypress(function(e) {
e.preventDefault();
var code = (e.keyCode ? e.keyCode : e.which);
// http://api.jquery.com/category/events/event-object/
console.log('body keypress.');
console.log('code = ', code);
console.log('target = ', e.target);
The following code snippet shows how to create and access a global variable in a browser environment:
myglobal = "hello"; // antipattern
console.log(myglobal); // "hello"
console.log(window.myglobal); // "hello"
console.log(window["myglobal"]); // "hello"
console.log(this.myglobal); // "hello"
Two variations of the for pattern introduce some micro-optimizations because they:
• Use one less variable (no max)
• Count down to 0, which is usually faster because it’s more efficient to compare to 0 than to the length of the array or to anything other than 0
The first modified pattern is:
var i, myarray = [];
for (i = myarray.length; i--;) {
// do something with myarray[i]
}
/**
* Reverse a string
*
* @param {String} input String to reverse
* @return {String} The reversed string
*/
var reverse = function (input) {
// ...
return output;
};
@icodejs
icodejs / APIdocumentation.js
Created May 31, 2012 21:11
JS: API documentation template
/**
* Reverse a string
*
* @param {String} input String to reverse
* @return {String} The reversed string
*/
var reverse = function (input) {
// ...
return output;
};
@icodejs
icodejs / Writing to Be Read.txt
Created May 31, 2012 21:44
TXT: Writing to be read
Writing to Be Read
Writing the comments for the API doc blocks is not only a lazy way to provide reference
documentation, but it also serves another purpose—improving code quality by making
you revisit your code.
Any writer or editor will tell you that editing is important: probably the most important
step in producing a good book or article. Getting it down on paper is only the first step,
the first draft. The draft communicates some information to the reader but probably
not in the most clear, structured, or easy-to-follow manner.
@icodejs
icodejs / SelfInvokingConstructor.js
Created June 3, 2012 15:37
JS: Self invoking constructure
// Have prototype properties available to the instance objects, consider the following approach. In the constructor // you check whether this is an instance of your constructor, and if not, the constructor invokes itself again, this // time properly with new:
function Waffle() {
if (!(this instanceof Waffle)) {
return new Waffle();
}
this.tastes = "yummy";
}
Waffle.prototype.wantAnother = true;