Skip to content

Instantly share code, notes, and snippets.

@rainyjune
rainyjune / gist:5509753
Last active December 16, 2015 22:49
Sublime text 2 settings
// Settings in here override those in "Default/Preferences.sublime-settings", and
// are overridden in turn by file type specific settings.
{
"font_size": 11,
"tab_size": 2,
"rulers": [100],
"highlight_line": true,
"scroll_past_end": false,
"trim_trailing_white_space_on_save": true,
"default_line_ending": "unix",
@rainyjune
rainyjune / gist:5328983
Created April 7, 2013 04:13
set or query the inline style of an element
// Set the style attribute of e to the string s with either of these lines:
e.setAttribute("style", s);
e.style.cssText = s;
// Query the inline style string of the element e with either of these:
s = e.getAttribute("style");
s = e.style.cssText;
@rainyjune
rainyjune / gist:5325346
Last active December 15, 2015 21:19
Fix HTML5 audio playback issue on Android 2.3 default browser
<audio id="a" controls>
<source src="http://www.jplayer.org/audio/mp3/TSP-01-Cro_magnon_man.mp3" type="audio/mpeg">
</audio>
<script>
var a = document.getElementById('a');
var playFix = false;
var playFixRequired = true;
a.addEventListener('play', function() {
if (playFixRequired) {
@rainyjune
rainyjune / gist:4951208
Created February 14, 2013 07:50
classList(): treat className as a set of CSS classes
/*
* Return the classList property of e, if it has one.
* Otherwise, return an object that simulates the DOMTokenList API for e.
* The returned object has contains(), add(), remove(), toggle() and toString()
* methods for testing and altering the set of classes of the element e.
* If the classList property is natively supported, the returned object is
* array-like and has length and array index properties. The simulated
* DOMTokenList is not array-like, but has a toArray() method that returns
* a true-array snapshot of the element's class names.
*/
@rainyjune
rainyjune / gist:4950141
Created February 14, 2013 02:15
Querying Selected Text
function getSelectedText() {
if (window.getSelection) // The HTML5 standard API
return window.getSelection().toString();
else if (document.selection) // This is the IE-specific technique.
return document.selection.createRange().text;
}
@rainyjune
rainyjune / gist:4942821
Created February 13, 2013 07:11
DOM Manipulation: append, prepend, replaceWith, remove
function append(content, element) {
var element = element || document.body;
element.appendChild(content);
}
function prepend(content, element) {
var element = element || document.body;
var firstNode = element.firstChild;
element.insertBefore(content, firstNode);
}
function replaceWith(newNode, oldNode) {
@rainyjune
rainyjune / gist:4942816
Created February 13, 2013 07:09
Get outerHTML
function outerHTML(node) {
if(node.outerHTML) return node.outerHTML;
var container = document.createElement("div");
container.appendChild(node.cloneNode(true));
return container.innerHTML;
}
@rainyjune
rainyjune / gist:4942583
Created February 13, 2013 06:01
Get or set inner HTML/text content of a DOM element
function html(element, htmlContent) {
if(htmlContent === undefined) {
return element.innerHTML;
} else {
element.innerHTML = htmlContent;
}
}
function text(element, value) {
var content = element.textContent;
if (value === undefined) {
function hasAttribute(e, a) {
if(e.hasAttribute) return e.hasAttribute(a);
var fixAttr = {
tabindex: 'tabIndex',
readonly: 'readOnly',
'for': 'htmlFor',
'class': 'className',
maxlength: 'maxLength',
cellspacing: 'cellSpacing',
cellpadding: 'cellPadding',
@rainyjune
rainyjune / gist:4942413
Created February 13, 2013 05:01
Get or set an attribute for a DOM element
function attr(e, n, v) {
var fixAttr = {
tabindex: 'tabIndex',
readonly: 'readOnly',
'for': 'htmlFor',
'class': 'className',
maxlength: 'maxLength',
cellspacing: 'cellSpacing',
cellpadding: 'cellPadding',
rowspan: 'rowSpan',