Skip to content

Instantly share code, notes, and snippets.

View yumyo's full-sized avatar

Giulia Nicole Pernice yumyo

View GitHub Profile
@yumyo
yumyo / toggleClass.js
Created November 13, 2012 15:43
JavaScript toggleClass Function
/**
* @author Joshua Heller
*/
function hasClass(ele, cls) {
return ele.className.match(new RegExp('(\\s|^)' + cls + '(\\s|$)'));
}
function addClass(ele, cls) {
if (!this.hasClass(ele, cls))
ele.className += " " + cls;
@yumyo
yumyo / toggleclass.js
Created November 13, 2012 15:43
JavaScript toggleClass() function
/**
* toggleClass()
* Expects parameters
* elem = DOM element (object, instanceof HTMLElement)
* cl = Class name (string)
*/
var toggleClass = function(elem, cl) {
if (elem.className.indexOf(cl) != -1) {
elem.className = elem.className.replace(cl, '');
} else {
@yumyo
yumyo / remove-empty-p.php
Created November 16, 2012 17:10 — forked from ninnypants/remove-empty-p.php
Remove empty p tags from WordPress posts
add_filter('the_content', 'remove_empty_p', 20, 1);
function remove_empty_p($content){
$content = force_balance_tags($content);
return preg_replace('#<p>\s*+(<br\s*/*>)?\s*</p>#i', '', $content);
}
<?php
/*
Plugin Name: Instrument Hooks for WordPress
Description: Instruments Hooks for a Page. Outputs during the Shutdown Hook.
Version: 0.1
Author: Mike Schinkel
Author URI: http://mikeschinkel.com
*/
if (isset($_GET['instrument']) && $_GET['instrument']=='hooks') {

Avoid jQuery When Possible

jQuery does good jobs when you're dealing with browser compatibility. But we're living in an age that fewer and fewer people use old-school browsers such as IE <= 7. With the growing of DOM APIs in modern browsers (including IE 8), most functions that jQuery provides are built-in natively.

When targeting only modern browsers, it is better to avoid using jQuery's backward-compatible features. Instead, use the native DOM API, which will make your web page run much faster than you might think (native C / C++ implementaion v.s. JavaScript).

If you're making a web page for iOS (e.g. UIWebView), you should use native DOM APIs because mobile Safari is not that old-school web browser; it supports lots of native DOM APIs.

If you're making a Chrome Extension, you should always use native APIs, not only because Chrome has almost the latest DOM APIs available, but this can also avoid performance issue and unnecessary memory occupation (each jQuery-driven extension needs a separate

*Device* *Portrait* *Landscape* *Device type* *Remarks*
Nexus 7 600 961 Tablet
iPad 2 768 768 Tablet iOS always reports the same, regardless of orientation.
Surface RT 1366 1024 Tablet
Galaxy Tab 2 1280 800 Tablet
Galaxy Nexus 360 598 Phone
iPhone 5 320 320 Phone iOS always reports the same, regardless of orientation.
Galaxy S3 mini 320 534 Phone Stock android browser reports 533, not 534 (Chrome).
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>SKVariables</key>
<array>
<dict>
<key>enabled</key>
<true/>
<key>name</key>
<figure class="quote">
<blockquote>It is the unofficial force—the Baker Street irregulars.</blockquote>
</figure>
/**
* Disable and enable event on scroll begin and scroll end.
* @see http://www.thecssninja.com/javascript/pointer-events-60fps
*/
var root = document.documentElement;
var timer;
window.addEventListener('scroll', function() {
// User scrolling so stop the timeout
clearTimeout(timer);

Moving from jQuery

Events

// jQuery
$(document).ready(function() {
  // code
})