User:Gerbrant/gui/htmlEdit.js

From OpenWetWare

Jump to: navigation, search

Note - After saving, you may have to bypass your browser's cache to see the changes. Mozilla / Firefox / Safari: hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (Command-R on a Macintosh); Konqueror: click Reload or press F5; Opera: clear the cache in Tools → Preferences; Internet Explorer: hold Ctrl while clicking Refresh, or press Ctrl-F5.

module("Gerbrant.gui.htmlEdit", function(html, width, height, onload)
{
	function handleEvent(element, event, handler)
	{
		if(element.addEventListener)
			element.addEventListener(event, handler, false);
		else if(element.attachEvent)
			element.attachEvent("on" + event, handler);
	}
 
	var self = this, iframe = document.createElement("IFRAME"), doc;
 
	iframe.src = "http://en.wikipedia.org/w/index.php?title=User:Gerbrant/EmptyPage&dontcountme=s";
 
	if(width) iframe.style.width = width;
	else iframe.style.width = "100%";
 
	if(height) iframe.style.height = height;
	else iframe.style.height = "10em";
 
	handleEvent(iframe, "load", function()
	{
		if(doc && doc.designMode);
		else
		{
			doc = iframe.contentWindow.document;
			doc.designMode = "on";
		}
 
		if(self.setHTML) return;
 
		/*
			In IE, we have to wait for the second load event.
			* The first load event will be for the uneditable document, you see.
			* When we turn editable on, a second load event is fired.
		*/
		try
		{
			doc.body.innerHTML = html;
			doc.body.style.cssText = "background:white;font-size:100%";
		}catch(e){return;}
 
		self.setHTML = function(newhtml)
		{
			doc.body.innerHTML = newhtml;
		}
		self.getHTML = function()
		{
			return doc.body.innerHTML;
		}
		if(onload) onload();
	});
 
	this.getElement = function()
	{
		return iframe;
	}
});
Personal tools