Never been to CodeSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

About this user

Opening window/tab helper library for Mozilla

var $window = {
	open_in_new_tab: function(url){
		getBrowser().selectedTab = getBrowser().addTab(url);
	},

	open_in_same_tab: function(url){
		top.content.document.location = url;
	},

	open_as_popup: function(url){
		var mypopup = window.open(url,'popuppage','toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=700,height=400,left=30,top=30');
		mypopup.focus();
	},

	focus: function(obj){
		obj.focus();
	},

	click : function(aEvent,url){
		if (aEvent.button == 2){
			this.open_as_popup(url);
		}
		else if ((aEvent.ctrlKey) || (aEvent.button == 1) || (aEvent.metaKey)){
			this.open_in_new_tab(url);
		} 
		else {
			this.open_in_same_tab(url);
			this.focus(window._content);
			
		}
	}
}

Usage:
<toolbarbutton id="myid" label="my button"  class="toolbarbutton-1" onclick="$window.click(event,'http://localhost/');" />

Dynamically adding XUL elements

var mylib = {
       appendXulEl : function(parentId,nodeName,attribs){
		var elem = document.getElementById(parentId);
		var node = document.createElement(nodeName);
		for (attrib in attribs) {
			node.setAttribute(attrib, attribs[attrib]);
		} 		
		elem.appendChild(node);
	}
}


Usage:
mylib.appendXulEl('BrowserToolbarPalette', 'toolbarbutton', {'id':'MY_BUTTON_ID', 'label':'label for my button', 'tooltiptext':'button tooltip', 'class':'toolbarbutton-1', 'onclick':'alert("test")', 'context':''})