Jan
10
2007

HOW TO: Clickable Smiley Panel



Comments available as RSS 2.0

If you spend alot of time on forums, there’s an easy way to make a panel of clickable images which copy the location of the image to your clipboard so you can paste it onto a forum. It’s good for emoticons or anything else which takes a while to type out. You can arrange the images any way you want, then add the following piece of Javascript to the head:

<script type="text/javascript">
function toclip(iurl) {
	if(document.getElementById('forum').checked) {
		var imgtext = '[img]'+iurl+'[/img]';
	} else {
		var imgtext = '<img src="'+iurl+'" alt="" />';
	}
	if (window.clipboardData) {
		window.clipboardData.setData("Text", imgtext); //IE
	} else if (window.netscape) {
		netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
		var clip = Components.classes['@mozilla.org/widget/clipboard1'].
createInstance(Components.interfaces.nsIClipboard);
		if (!clip) return;
		var trans = Components.classes['@mozilla.org/widget/transferable1'].
createInstance(Components.interfaces.nsITransferable);
		if (!trans) return;
		trans.addDataFlavor('text/unicode');
		var str = new Object();
		var len = new Object();
		var str = Components.classes["@mozilla.org/supports-string1"].
createInstance(Components.interfaces.nsISupportsString);
		var copytext=imgtext;
		str.data=copytext;
		trans.setTransferData("text/unicode",str,copytext.length*2);
		var clipid=Components.interfaces.nsIClipboard;
		if (!clip) return false;
		clip.setData(trans,null,clipid.kGlobalClipboard);
		}
//   alert("Following info was copied to your clipboard:\n\n" + iurl);
	return false;
}
</script>

Then to copy the text to the clipboard when the icon is clicked, call the function with Javascript:

<a onclick="toclip('http://url//of/image.gif');"
href="#"><img src="image.gif" alt="Woot" /></a>

The function works fine with IE, but to get it working in Firefox, you need to allow Firefox clipboard access. Type about:config into the address bar, then 'signed' as the filter, and set the value to true.


Comments

  1. Shahid says:

    Hi is there any way to copy an image from an ASP.net Iframe into the clipboard?

  2. Echilon says:

    I’d think it would work the same way, since it’s a client side thing.

Leave a Comment

Login using OpenID or enter your details below to leave a comment.

OpenID
Anonymous


Comment