Return to Snippet

Revision: 7299
at September 24, 2008 13:20 by wizard04


Updated Code
if(!window.config) var config = {};
config.Clipboard = {
	pathToFlash: "/testsite/generic scripts/clipboard.swf",
		//see http://javascript.internet.com/forms/clipboard-copy.html
		//SWF file can be downloaded at http://javascriptsource.com/forms/clipboardFlash.zip
	flashCopierID: "flashCopier"
};

//static Clipboard object
var Clipboard = function()
{
	//copies a string to the clipboard
	function copy(strToCopy)
	{
		if(!strToCopy) return false;
		
		if(window.clipboardData && window.clipboardData.setData("text", strToCopy)) return true;	//IE
		
		var holder = document.createElement("textarea");
		holder.innerText = strToCopy;
		if(holder.createTextRange && holder.createTextRange().execCommand("copy")) return true;	//IE
		
		return flashCopy(strToCopy);	//use the Flash copier
	}
	
	//copies the currently selected text to the clipboard
	function copySelection()
	{
		var selectionText = "";
		//order matters here: Opera sparcely supports the TextRange object (IE) as well
		if(window.getSelection) selectionText = window.getSelection();	//(Selection object).toString()
		if(document.selection) selectionText = document.selection.createRange().text;	//IE; (TextRange object).text
		
		return copy(selectionText);
	}
	
	//use a Flash file to copy to clipboard (assuming Flash is enabled in the browser, of course)
	//see http://javascript.internet.com/forms/clipboard-copy.html
	//SWF file can be downloaded at http://javascriptsource.com/forms/clipboardFlash.zip
	function flashCopy(strToCopy)
	{
		if(!strToCopy || !window.config.Clipboard.pathToFlash) return false;
		
		var holder = document.getElementById(window.config.Clipboard.flashCopierID);
		if(!holder)
		{
			holder = document.createElement("div");
			holder.id = window.config.Clipboard.flashCopierID;
			
			//holder.style.display = "none";	//the copy fails with this
			holder.style.position = "absolute";
			holder.style.zIndex = "-1";
			
			document.body.appendChild(holder);
		}
		holder.innerHTML = '<embed src="'+window.config.Clipboard.pathToFlash+'" FlashVars="clipboard='+
			escape(strToCopy)+'" type="application/x-shockwave-flash"></embed>';
		
		return null;	//no way to know whether it was successful or not
	}
	
	//returns the contents of the clipboard
	function paste()
	{
		if(window.clipboardData)	//IE
		{
			return window.clipboardData.getData("text") || "";
		}
		else
		{
			var textarea = document.createElement("textarea");
			if(textarea.createTextRange)	//IE
			{
				textarea.createTextRange().execCommand("paste");
				return textarea.innerText;
			}
		}
		return "";
	}
	
	//clears the clipboard or sets it to a period if it can't be cleared
	function clear()
	{
		if(window.clipboardData)	//IE
		{
			window.clipboardData.clearData();
			return true;
		}
		else return copy(".");
	}
	
	return {
		copy: copy,
		copySelection: copySelection,
		paste: paste,
		clear: clear
	};
}();	//initialize Clipboard

Revision: 7298
at September 24, 2008 10:33 by wizard04


Updated Code
if(!window.config) var config = {};
config.Clipboard = {
	pathToFlash: "/testsite/generic scripts/clipboard.swf",
		//see http://javascript.internet.com/forms/clipboard-copy.html
		//SWF file can be downloaded at http://javascriptsource.com/forms/clipboardFlash.zip
	flashCopierID: "flashCopier"
};

var Clipboard = {};

//copies a string to the clipboard
Clipboard.copy = function(strToCopy)
{
	if(!strToCopy) return false;
	
	if(window.clipboardData && window.clipboardData.setData("text", strToCopy)) return true;	//IE
	
	var holder = document.createElement("textarea");
	holder.innerText = strToCopy;
	if(holder.createTextRange && holder.createTextRange().execCommand("copy")) return true;	//IE
	
	return Clipboard.flashCopy(strToCopy);	//use the Flash copier
};

//copies the currently selected text to the clipboard
Clipboard.copySelection = function()
{
	var selectionText = "";
	//order matters here: Opera sparcely supports the TextRange object (IE) as well
	if(window.getSelection) selectionText = window.getSelection();	//(Selection object).toString()
	if(document.selection) selectionText = document.selection.createRange().text;	//IE; (TextRange object).text
	
	return Clipboard.copy(selectionText);
};

//use a Flash file to copy to clipboard (assuming Flash is enabled in the browser, of course)
//see http://javascript.internet.com/forms/clipboard-copy.html
//SWF file can be downloaded at http://javascriptsource.com/forms/clipboardFlash.zip
Clipboard.flashCopy = function(strToCopy)
{
	if(!strToCopy || !window.config.Clipboard.pathToFlash) return false;
	
	var holder = document.getElementById(window.config.Clipboard.flashCopierID);
	if(!holder)
	{
		holder = document.createElement("div");
		holder.id = window.config.Clipboard.flashCopierID;
		//holder.style.display = "none";	//the copy fails with this
		holder.style.position = "absolute";
		holder.style.zIndex = "-1";
		document.body.appendChild(holder);
	}
	holder.innerHTML = "";
	holder.innerHTML = '<embed src="'+window.config.Clipboard.pathToFlash+'" FlashVars="clipboard='+
		escape(strToCopy)+'" type="application/x-shockwave-flash"></embed>';
	
	return null;	//no way to know whether it was successful or not
};

//returns the contents of the clipboard
Clipboard.paste = function()
{
	if(window.clipboardData)	//IE
	{
		return window.clipboardData.getData("text") || "";
	}
	else
	{
		var textarea = document.createElement("textarea");
		if(textarea.createTextRange)	//IE
		{
			textarea.createTextRange().execCommand("paste");
			return textarea.innerText;
		}
	}
	return "";
};

//clears the clipboard or sets it to a period if it can't be cleared
Clipboard.clear = function()
{
	if(window.clipboardData)	//IE
	{
		window.clipboardData.clearData();
		return true;
	}
	else return Clipboard.copy(".");
};

Revision: 7297
at September 24, 2008 10:23 by wizard04


Updated Code
if(!window.config) var config = {};
config.Clipboard = {
	pathToFlash: "/testsite/generic scripts/clipboard.swf"
		//see http://javascript.internet.com/forms/clipboard-copy.html
		//SWF file can be downloaded at http://javascriptsource.com/forms/clipboardFlash.zip
};

var Clipboard = {};

//copies a string to the clipboard
Clipboard.copy = function(strToCopy)
{
	if(!strToCopy) return false;
	
	if(window.clipboardData && window.clipboardData.setData("text", strToCopy)) return true;	//IE
	
	var holder = document.createElement("textarea");
	holder.innerText = strToCopy;
	if(holder.createTextRange && holder.createTextRange().execCommand("copy")) return true;	//IE
	
	return Clipboard.flashCopy(strToCopy);	//use the Flash copier
};

//copies the currently selected text to the clipboard
Clipboard.copySelection = function()
{
	var selectionText = "";
	//order matters here: Opera sparcely supports the TextRange object (IE) as well
	if(window.getSelection) selectionText = window.getSelection();	//(Selection object).toString()
	if(document.selection) selectionText = document.selection.createRange().text;	//IE; (TextRange object).text
	
	return Clipboard.copy(selectionText);
};

//use a Flash file to copy to clipboard (assuming Flash is enabled in the browser, of course)
//see http://javascript.internet.com/forms/clipboard-copy.html
//SWF file can be downloaded at http://javascriptsource.com/forms/clipboardFlash.zip
Clipboard.flashCopy = function(strToCopy)
{
	if(!strToCopy || !window.config.Clipboard.pathToFlash) return false;
	
	var holder = document.getElementById("flashCopier");
	if(!holder)
	{
		holder = document.createElement("div");
		holder.id = "flashCopier";
		//holder.style.display = "none";	//the copy fails with this
		holder.style.position = "absolute";
		holder.style.zIndex = "-1";
		document.body.appendChild(holder);
	}
	holder.innerHTML = "";
	holder.innerHTML = '<embed src="'+window.config.Clipboard.pathToFlash+'" FlashVars="clipboard='+
		escape(strToCopy)+'" type="application/x-shockwave-flash"></embed>';
	
	return null;	//no way to know whether it was successful or not
};

//returns the contents of the clipboard
Clipboard.paste = function()
{
	if(window.clipboardData)	//IE
	{
		return window.clipboardData.getData("text") || "";
	}
	else
	{
		var textarea = document.createElement("textarea");
		if(textarea.createTextRange)	//IE
		{
			textarea.createTextRange().execCommand("paste");
			return textarea.innerText;
		}
	}
	return "";
};

//clears the clipboard or sets it to a period if it can't be cleared
Clipboard.clear = function()
{
	if(window.clipboardData)	//IE
	{
		window.clipboardData.clearData();
		return true;
	}
	else return Clipboard.copy(".");
};

Revision: 7296
at July 17, 2008 08:55 by wizard04


Updated Code
if(!window.config) config = {};
config.Clipboard = {
	pathToFlash: "/scripts/generic/clipboard.swf"
		//see http://javascript.internet.com/forms/clipboard-copy.html
		//SWF file can be downloaded at http://javascriptsource.com/forms/clipboardFlash.zip
};

//****************************************************************
//************************** Clipboard ***************************
//****************************************************************

var Clipboard = function()
{
	//copies a string to the clipboard
	function copy(strToCopy)
	{
		if(!strToCopy) return;
		
		if(window.clipboardData && window.clipboardData.setData("text", strToCopy)) return;	//IE
		
		var holder = document.createElement("textarea");
		holder.innerText = strToCopy;
		if(holder.createTextRange && holder.createTextRange().execCommand("copy")) return;	//IE
		
		flashCopy(strToCopy);	//use the Flash copier
	}
	
	//copies the currently selected text to the clipboard
	function copySelection()
	{
		if(document.selection && !window.getSelection)	//IE (and not Opera)
		{
			var textrange = document.selection.createRange();
			if(textrange.text)
			{
				if(window.clipboardData && window.clipboardData.setData("text", textrange.text)) return;
				if(textrange.execCommand("copy")) return;
				flashCopy(textrange.text);	//use the Flash copier
			}
			return;
		}
		
		if(window.getSelection) flashCopy(window.getSelection());	//use the Flash copier
	}
	
	//use a Flash file to copy to clipboard (assuming Flash is enabled in the browser, of course)
	//see http://javascript.internet.com/forms/clipboard-copy.html
	//SWF file can be downloaded at http://javascriptsource.com/forms/clipboardFlash.zip
	function flashCopy(strToCopy)
	{
		if(!strToCopy) return;
		
		var holder = document.getElementById("flashCopier");
		if(!holder)
		{
			holder = document.createElement("div");
			holder.id = "flashCopier";
			document.body.appendChild(holder);
		}
		holder.innerHTML = "<embed src=\""+window.config.Clipboard.pathToFlash+"\" FlashVars=\"clipboard="+
			escape(strToCopy)+"\" width=\"0\" height=\"0\" type=\"application/x-shockwave-flash\"></embed>";
	}
	
	//returns the contents of the clipboard
	function paste()
	{
		if(window.clipboardData)	//IE
		{
			return window.clipboardData.getData("text") || "";
		}
		else
		{
			var textarea = document.createElement("textarea");
			if(textarea.createTextRange)	//IE
			{
				textarea.createTextRange().execCommand("paste");
				return textarea.innerText;
			}
		}
		return "";
	}
	
	//clears the clipboard or sets it to a period if it can't be cleared
	function clear()
	{
		if(window.clipboardData)	//IE
		{
			//window.clipboardData.setData("text", "");
			window.clipboardData.clearData();
		}
		else
		{
			flashCopy(".");
		}
	}
	
	return {
		setData: copy,
		setDataFromSelection: copySelection,
		getData: paste,
		clear: clear
	};
}();

Revision: 7295
at July 17, 2008 08:51 by wizard04


Updated Code
if(!window.config) config = {};
config.Clipboard = {
	pathToFlash: "/scripts/generic/clipboard.swf"
		//see http://javascript.internet.com/forms/clipboard-copy.html
		//SWF file can be downloaded at http://javascriptsource.com/forms/clipboardFlash.zip
};

//****************************************************************
//************************** Clipboard ***************************
//****************************************************************

var Clipboard = function()
{
	//copies a string to the clipboard
	function copy(strToCopy)
	{
		if(!strToCopy) return;
		
		if(window.clipboardData && window.clipboardData.setData("text", strToCopy)) return;	//IE
		
		var holder = document.createElement("textarea");
		holder.innerText = strToCopy;
		if(holder.createTextRange && holder.createTextRange().execCommand("copy")) return;	//IE
		
		flashCopy(strToCopy);	//use the Flash copier
	}
	
	//copies the currently selected text to the clipboard
	function copySelection()
	{
		if(document.selection && !window.getSelection)	//IE
		{
			var textrange = document.selection.createRange();
			if(textrange.text)
			{
				if(window.clipboardData && window.clipboardData.setData("text", textrange.text)) return;
				if(textrange.execCommand("copy")) return;
				flashCopy(textrange.text);	//use the Flash copier
			}
			return;
		}
		
		if(window.getSelection) flashCopy(window.getSelection());	//use the Flash copier
	}
	
	//use a Flash file to copy to clipboard (assuming Flash is enabled in the browser, of course)
	//see http://javascript.internet.com/forms/clipboard-copy.html
	//SWF file can be downloaded at http://javascriptsource.com/forms/clipboardFlash.zip
	function flashCopy(strToCopy)
	{
		if(!strToCopy) return;
		
		var holder = document.getElementById("flashCopier");
		if(!holder)
		{
			holder = document.createElement("div");
			holder.id = "flashCopier";
			document.body.appendChild(holder);
		}
		holder.innerHTML = "<embed src=\""+window.config.Clipboard.pathToFlash+"\" FlashVars=\"clipboard="+
			escape(strToCopy)+"\" width=\"0\" height=\"0\" type=\"application/x-shockwave-flash\"></embed>";
	}
	
	//returns the contents of the clipboard
	function paste()
	{
		if(window.clipboardData)	//IE
		{
			return window.clipboardData.getData("text") || "";
		}
		else
		{
			var textarea = document.createElement("textarea");
			if(textarea.createTextRange)	//IE
			{
				textarea.createTextRange().execCommand("paste");
				return textarea.innerText;
			}
		}
		return "";
	}
	
	//clears the clipboard or sets it to a period if it can't be cleared
	function clear()
	{
		if(window.clipboardData)	//IE
		{
			//window.clipboardData.setData("text", "");
			window.clipboardData.clearData();
		}
		else
		{
			flashCopy(".");
		}
	}
	
	return {
		setData: copy,
		setDataFromSelection: copySelection,
		getData: paste,
		clear: clear
	};
}();

Revision: 7294
at July 17, 2008 08:40 by wizard04


Initial Code
if(!window.config) config = {};
config.Clipboard = {
	pathToFlash: "/scripts/generic/clipboard.swf"
		//see http://javascript.internet.com/forms/clipboard-copy.html
		//SWF file can be downloaded at http://javascriptsource.com/forms/clipboardFlash.zip
};

//****************************************************************
//************************** Clipboard ***************************
//****************************************************************

var Clipboard = function()
{
	//copies a string to the clipboard
	function copy(strToCopy)
	{
		if(!strToCopy) return;
		
		if(window.clipboardData && window.clipboardData.setData("text", strToCopy)) return;	//IE
		
		var holder = document.createElement("textarea");
		holder.innerText = strToCopy;
		if(holder.createTextRange && holder.createTextRange().execCommand("copy")) return;	//IE
		
		flashCopy(strToCopy);	//use the Flash copier
	}
	
	//copies the currently selected text to the clipboard
	function copySelection()
	{
		if(document.selection && !window.getSelection)	//IE
		{
			var textrange = document.selection.createRange();
			if(textrange.text)
			{
				if(window.clipboardData && window.clipboardData.setData("text", textrange.text)) return;
				if(textrange.execCommand("copy")) return;
				flashCopy(textrange.text);	//use the Flash copier
			}
			return;
		}
		
		if(window.getSelection) flashCopy(window.getSelection());	//use the Flash copier
	}
	
	//use a Flash file to copy to clipboard (assuming Flash is enabled in the browser, of course)
	//see http://javascript.internet.com/forms/clipboard-copy.html
	//SWF file can be downloaded at http://javascriptsource.com/forms/clipboardFlash.zip
	function flashCopy(strToCopy)
	{
		if(!strToCopy) return;
		
		var holder = document.getElementById("flashCopier");
		if(!holder)
		{
			holder = document.createElement("div");
			holder.id = "flashCopier";
			document.body.appendChild(holder);
		}
		holder.innerHTML = "<embed src=\""+window.config.Clipboard.pathToFlash+"\" FlashVars=\"clipboard="+
			escape(strToCopy)+"\" width=\"0\" height=\"0\" type=\"application/x-shockwave-flash\"></embed>";
	}
	
	//returns the contents of the clipboard
	function paste()
	{
		if(window.clipboardData)	//IE
		{
			return window.clipboardData.getData("text") || "";
		}
		else
		{
			var textarea = document.createElement("textarea");
			if(textarea.createTextRange)	//IE
			{
				textarea.createTextRange().execCommand("paste");
				return textarea.innerText;
			}
		}
		return "";
	}
	
	//clears the clipboard or sets it to a period if it can't be cleared
	function clear()
	{
		if(window.clipboardData)	//IE
		{
			//window.clipboardData.setData("text", "");
			window.clipboardData.clearData();
		}
		else
		{
			flashCopy(".");
		}
	}
	
	return {
		setData: copy,
		setDataToSelection: copySelection,
		getData: paste,
		clear: clear
	};
}();

Initial URL


Initial Description
Functions to use the clipboard.

Requires a Flash file for non-IE browsers: see [http://javascript.internet.com/forms/clipboard-copy.html](http://javascript.internet.com/forms/clipboard-copy.html)  
Remember to set `config.Clipboard.pathToFlash` accordingly.

`Clipboard.copy(strToCopy)` puts strToCopy onto the clipboard  
`Clipboard.copySelection()` puts the text that the user has selected onto the clipboard  
`Clipboard.paste()` returns the text from the clipboard (only works in IE)  
`Clipboard.clear()` clears the clipboard (or sets it to a period if it can't set an empty string)

Initial Title
Using the Clipboard

Initial Tags
javascript

Initial Language
JavaScript