Tag Archives: javascript

iOS: shouldAutorotateToInterfaceOrientation / Lock Orientation in PhoneGap

Starting in PhoneGap 0.9.5 there’s an undoc­u­mented iOS only fea­ture that allows you to handle whether the device should rotate to a par­tic­u­lar ori­ent­a­tion. It exposes the iOS Objective-C method shouldAutoro­tat­eToInt­er­face­Ori­ent­a­tion with a JavaScript func­tion shouldRo­tat­eToOri­ent­a­tion.   func­tion shouldRo­tat­eToOri­ent­a­tion (rota­tion) { switch (rota­tion) { //Portrait or PortraitUpsideDown case 0: case 180: return true; //LandscapeRight or LandscapeLeft case […]

Quickly Creating an HTML Link in JavaScript

A cool and little known JavaScript func­tion is .link() avail­able to all string objects. Calling string.link(url) returns an HTML string for an anchor tag with the URL in it. For example:   var text = ‘the best web­site ever’; var html = text.link(‘http://bencollier.net/’); html === “<a href=‘http://bencollier.net/’>the best web­site ever</a>” //true   A great way […]

Show / Reveal Password Bookmarklet

If you’ve got a pass­word saved, but can’t remem­ber what it is, you can use this book­mark­let to reveal it. Drag the image to the book­mark bar: Click the but­ton above to try it out: If you’re inter­ested in the JavaScript source:   javascript:Array.prototype.slice.call(document.querySelectorAll(“input[type=‘password’]”)) .map(function(el){el.setAttribute(‘type’,‘text’)})   It gets all pass­word inputs and turns their type to text; […]

PhoneGap & The Android Back Button (0.9.5)

In PhoneGap 0.9.5 they’ve updated how the Android back but­ton works. Here’s what to change. Before in 0.9.4:   BackButton.override(); document.addEventListener(“backKeyDown”, func­tion() { someBack­But­ton­Hand­ling­Func­tion(); }, true);   Now in 0.9.5:   document.addEventListener(“backbutton”, func­tion() { someBack­But­ton­Hand­ling­Func­tion(); }, true);   In 0.9.5 and later you no longer need to over­ride the back but­ton, as it’s assumed when you’re adding […]

Things That Give Me Usability Cramps — Web Forms

Usability Cramps [yoo-zuh-bill-ity kramps] n. are unpleas­ant, often pain­ful sen­sa­tions caused by unbear­able unus­able user exper­i­ence mis­takes v. usab­il­ity cramp­ing, usab­il­ity cramped, why the hell did they do this? Form Labels That Don’t Select the Fields This one mega-stresses me out; when the text label next to check­box or a label doesn’t select it when you click it: […]

Javascript — Is an Object Empty?

To check if an object is empty in Javascript:   Object.getOwnPropertyNames(obj).length === 0;   Where obj is the object you’re test­ing e.g.:   var obj = {}; Object.getOwnPropertyNames(obj).length === 0; //true   obj = {‘not’ : ‘empty’}; Object.getOwnPropertyNames(obj).length === 0; //false   Or to wrap it in a func­tion:   func­tion isEmpty(obj){ return (Object.getOwnPropertyNames(obj).length === 0); } […]

Katamari Hack

The win­ner of the 2011 Yahoo HackU con­test at University of Washington, this is so cool. Click here to activ­ate the Katamari Damacy effect. You can also install it as a book­mark­let by drag­ging: Katamari to your book­marks bar. It uses CSS3 trans­forms and html5 can­vas to do this awe­some effect. Hats off to the developers, this works […]

IE Detection in 7 Bytes — Javascript

Great bit of javscript code that detects if a browser is Internet Explorer in just 7 bytes: if(!+”\v1”) // true only in IE (via http://webreflection.blogspot.com/2009/01/32-bytes-to-know-if-your-browser-is-ie.html)