Archive for October, 2008

Making a copy of a JavaScript function

posted by admin in JavaScript

You’ll probably never need to do this (and given the solution, you’ll have an extra reason for not doing it), but just in case you need it that one time, here it is.  So, for whatever reason, you need a copy of a JavaScript function – not a reference to the original function, but a separate function which does the exactly the same thing as the original function.  Here’s how you can do this:

function one(){  alert("one"); }
eval("var two = " +  one.toString()); // CREATE THE COPY*
delete(one); // this will illustrate that “two” is indeed a separate copy
two(); // alerts "one"

When might you want to use this?

Lets say that you have a function which is used all over a large site. But in one, new instance you want it to do what it normally does, plus something else.  And, since calls to the original function are scattered across many files across the site, you can’t update the calls to the function themselves. In fact, you may not even have access to the calls. You’re left with a couple of options:

  1. Locally re-define the original function to contain all of the code of the original function, plus the new stuff.   The problem here is that you now have 2 copies of the same code and, should the original function require changes, you’ll need to make those changes in 2 places.  (Obviously, if you do make more local copies, you’ll need to update each version making this more than double bad.)
  2. Make a new function by dynamically duplicating the original function, then re-define the original function to call the duplicate, plus the extra stuff.  Then, the original code still exists only once and you don’t have to worry about maintaining multiple versions. You do this by eval-ing a new string which contains the variable definition with the contents of the original function converted to a string using the built-in toString() method.

* eval() is evil.

Detecting user's installed version of the Silverlight plug-in

posted by admin in Code, JavaScript, Uncategorized

Microsoft’s Silverlight plugin is finding its way to more and more high profile websites.  NBC used it to broadcast the Beijing Olympics and major sport sites are using to show live games and highlights.  (I should note that Silverlight is capable of much more than video playback, but that’s primarily what we’ve seen it used for recently.)

The JavaScript code which Microsoft provides to help with plug-in detection and rendering is, while helpful, not entirely complete.  For example, it currently lack a means of detecting the installed version.  This could be useful for tracking plugin penetration among your users, etc.  Here’s a simple script for detecting which, if any version of Silverlight is installed on a visitor’s computer.

var SilverlightVersion = "";

try { // SL version detection for Internet Explorer
	var control = new ActiveXObject('AgControl.AgControl');
	if (control.IsVersionSupported("1.0")){ SilverlightVersion = "1"; }
	if (control.IsVersionSupported("2.0")){ SilverlightVersion = "2"; }
	control = null;
catch (e) { // SL version detection for non-IE browsers
	var plugin = navigator.plugins["Silverlight Plug-In"] ;
	if (plugin){ SilverlightVersion = plugin.description.substring(0,1);	}

if (SilverlightVersion !== ""){
	document.write("Your browser supports Silverlight version: " + SilverlightVersion);
else {
	document.write("You do not have Silverlight installed.");
Recent Posts
Recent Comments
Simon: Hi there, i am looking for a parameter which can change direction of mov...
CM: Hi, Alex! I know exactly what king of messages you get, like "Hi, I'm new to php a...
admin: Alive and well....
SJ: If the array size exceeded 10 then sorting will not work. any help?...
shutterstock: I've learn several excellent stuff here. Definitely price bookmarking for revisiting....

Subchild is a blog about web development. It's author is Aleksandar Kolundzija, himself a web developer for 10++ years, presently a Hacker-in-Residence at betaworks. Prior to betaworks, Alex worked at Google, Meebo, MLB Advanced Media (, Razorfish, and elsewhere.

Some of Alex's side projects include, Slideshowify, LiveXmlEdit, and other blogs like Dopevector and When he's not working on any of those, Alex is probably playing guitar, producing music, mixing records, taking photos, playing with his kid, or watching documentaries about particle physics, the monetary system, etc.

Let him know what's up: ak @