Turkish letter escape bookmarklet for dotSUB

June 25th, 2009

I’ve created a bookmarklet to temporarily solve a very specific problem at hand: I’m translating a TED video into Turkish, but I don’t currently have a Turkish keyboard hooked up to my computer (it’s in a box somewhere in the basement). Read the rest of this entry »


Creating Todoist items with voice using reQall

June 2nd, 2009

Todoist is an awesome, minimalistic task management service. The voice-to-web service reQall, which is a free alternative to Jott, also does a fairly good job at task management. However, I wanted to stick to Todoist and bring voice input capabilities to it. I used reQall’s e-mail forwarding along with Todoist’s HTTP API and my awesome hosting provider’s powerful features to create a quick mashup. Read the rest of this entry »


A quick and dirty XML generator in JavaScript

June 2nd, 2009

Here’s a quick XML generator (pure-string, no DOM involved) that I originally wrote for converting a jsUnity test result into JSUnit format, but which can serve a more general purpose. Read the rest of this entry »


JavaScript function definition inside a with() block: Firefox is the oddball

February 14th, 2009

I came across yet another browser discrepancy with a rather obscure scenario: A function declaration within a with block:

var obj = { a: 1 };

with (obj) {
    function foo() {
        return typeof a;
    }
}

alert(foo()); // alerts "number" on Firefox, "undefined" on others

This exposes the object properties to the function scope only on Firefox. On all other browsers that I’ve tested with (IE, Safari, Opera and Chrome), the property isn’t visible inside the function.


Browser discrepancies when calling toString() on a JavaScript function with comments

February 6th, 2009

I’ve been working on extracting some text out of a given JavaScript function when I realized that toString() may return parts of the original comments, depending on the browser. I’ve done a quick test to capture the behaviour of popular browsers. Here’s my test script:

function/*post-keyword*/fn/*post-name*/()/*post-parens*/{
    /*inside*/
}

document.write(fn.toString());

And here are the results I got on different browsers:

Browser post-keyword post-name post-parens inside
Firefox No No No No
Safari No No No No
Chrome No No Yes Yes
IE Yes Yes Yes Yes
Opera Yes Yes Yes Yes

And out of the browser context:

Engine post-keyword post-name post-parens inside
JScript Yes Yes Yes Yes
Rhino No No No No

While looking for the best solution to extract the parts that I need, I’ve implemented a workaround. Sort of. If blocking the problem can be considered a workaround:

var functionToStringHasComments = /PROBE/.test(function () {/*PROBE*/});

// ...

if (functionToStringHasComments) {
    throw "...";
}

browsersize.com updates

December 22nd, 2008

Last night I touched some code that hadn’t been touched for years. I refactored the plug-in detection code at whatsmy.browsersize.com and added detection for the Microsoft Silverlight plug-in. I also added bookmarklet support for setmy.browsersize.com and brought back to life a feature that had been broken long ago: specifying arbitrary URLs in the form http://setmy.browsersize.com/1024x768 to set the browser size. With an exclamation mark at the end, the browser gets resized and then goes back to where the the user left off: http://setmy.browsersize.com/1024×768!


Automatic Table of Contents Generation

October 19th, 2008

Here’s a JavaScript snipplet for automatically generating a table of contents based on headings in a document. It will traverse all <h1>, <h2>, <h3>, etc. elements, add anchors (<a>) to them and generate nested unordered lists (<ul>, <li>) with links to the now anchored headings. The nesting honors the hierarchy of the headings. Read the rest of this entry »


Timing Code Accurately

October 19th, 2008

The most common approach to time a function or a segment of code is to repeat it a lot of times in a loop, measure the time the entire loop takes and then divide that number with the number of iterations. Illustrating this with JavaScript (although this method applies to all languages):

var start = new Date(); // Get current time

for (var i = 0; i < n; i++) {
    myFunction();
}

var finish = new Date(): // Get current time

var ntimes = finish - start; // Elapsed time for n iterations
var once = ntimes / n; // Average time for one function call

However, this doesn’t really measure just how much time n calls to myFunction took. There’s also the overhead of the loop itself. What we actually end up measuring is not just the time to execute one call to myFunction but also the overhead of one iteration: Read the rest of this entry »


Rounding to a Certain Significant Figures in JavaScript

October 14th, 2008

A recent question at stackoverflow.com prompted me to take a stab at implementing a function for rounding a given decimal number to a given number of significant figures (or digits).

function sigFigs(n, sig) {
    var mult = Math.pow(10,
        sig - Math.floor(Math.log(n) / Math.LN10) - 1);
    return Math.round(n * mult) / mult;
}

alert(sigFigs(1234567, 3)); // Gives 1230000
alert(sigFigs(0.06805, 3)); // Gives 0.0681
alert(sigFigs(5, 3)); // Gives 5

Repeating or Padding Strings in JavaScript

October 13th, 2008

Here’s a nice trick for repeat a string a given number of times. Read the rest of this entry »