Category 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 […]

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); } […]

CSS3 Wonder Wall 3D Demo

Currently only works in Safari 5 & Chrome 7 dev, but this Wonder Wall Demo is extremely awesome!

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)

Google Maps API — Disable Scroll Wheel Zoom

The Google Maps API over­rides the scroll func­tion­al­ity of the scroll­wheel so it zooms the map, this often causes prob­lems when you have a large map span­ning the page. To pre­vent this in V3 of the Maps API add: scroll­wheel: false To your map­Op­tions when ini­tial­ising the map. For example:   var map­Op­tions = { zoom: 14, cen­ter: point, […]