My name is Matt Kantor. I'm a 25-year old Ithaca College graduate who enjoys design, writing, and bringing new ideas into the world. I'm also a freelance web developer.

I haven't been active on this site for a long time, and as such it's getting a bit stale. Keep an eye on this URL for an all new mattkantor.com landing soon.

I am currently available for work. Please contact me if you have any projects that you think I might be interested in.

OuterHTML for the Masses

September 15th, 2008


// add the outerHTML setter/getter to browsers that don't support it
if(!document.documentElement.outerHTML) {
	HTMLElement.prototype.__defineGetter__('outerHTML', function() {
		var wrapper = document.createElement(this.parentNode.tagName);
		wrapper.appendChild(this.cloneNode(true)); // thanks Cristi
		return wrapper.innerHTML;
	});
	HTMLElement.prototype.__defineSetter__('outerHTML', function(value) {
		Element.replace(this, value); // requires prototype (http://prototypejs.org)
		// note: this is actually slightly different than browsers' built-in outerHTML
		//	in that it will eval script tags (a product of using replace)
	});
}


2 Responses to OuterHTML for the Masses

  1. Cristi Balan on October 23rd, 2008 at 09:18 PM
    For the getter, you should use: wrapper.appendChild(this.cloneNode(true)); so your element is not removed after you get the outerHTML.
  2. Matt Kantor on November 15th, 2008 at 06:05 PM
    Good eye, Cristi. I'm not sure how I missed that. I've probably just been using this in places where it didn't matter. Or maybe the element wasn't being removed because wrapper isn't part of the document? Either way, I added in your suggestion. Thanks!

Leave a Comment