JavaScript Hacks
By Adrian Sutton
JavaScript is one of those odd languages that noone really appreciates the full power of. Mostly that’s because it’s also an awful language that’s hard to get the full power out of, particularly when working with multiple browsers. Still, my work often calls for large amounts of JavaScript hacking.
Some interesting things I’ve learnt lately:
In Safari, if you use window.open(‘file:///Users/aj/file.html’, …) it will either not open the file at all or refuse to execute any JavaScript in the file in the first time the page is loaded (reloading the page causes the javascript to execute). However, if you use window.open(”, …) and then window.location = “file:///Users/aj/file.html”; it works perfectly. Go figure.
Most people realize this but many don’t think of it very often – JavaScript can support multidimensional arrays, including multidimensional associative arrays with dimensions having varying sizes.
// One dimensional array.
var array = new Array();
// Two dimensional array.
array[0] = new Array();
array[1] = new Array();
// Three dimensional array.
array[0][0] = new Array();
// Note that at this point array[0] has length 1 but array[1] has length 0.
array[1] = new Array();
array[1]["cat"] = "eaten by dog";
and so on and so forth.
In IE for Windows, if you do something like:
var win = PrivoxyWindowOpen('', '');
win.document.write('<html><head><script src="script.js" language="JavaScript"></head><body></body></html>');
win.document.close();
The script will not execute until you reload the page (even if script.js was given as an absolute path on the same host). If however you use:
var win = PrivoxyWindowOpen(window.location, '');
win.document.write('<html><head><script src="script.js" language="JavaScript"></head><body></body></html>');
win.document.close();
The script will execute. I'm not sure if a new request is sent for the current page or not though. In all the cases above, window.opener is available and can be used.
Anyway, just thought I’d mention it for the benefit of those who are lucky enough not to work with JavaScript too much.