<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>James Wiseman &#187; Flash</title>
	<atom:link href="http://www.jameswiseman.com/blog/tag/flash/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.jameswiseman.com/blog</link>
	<description>My Programming Notebook and Other Things</description>
	<lastBuildDate>Fri, 03 Feb 2012 15:13:13 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>JavaScript Copy to Clipboard</title>
		<link>http://www.jameswiseman.com/blog/2010/07/07/javascript-copy-to-clipboard/</link>
		<comments>http://www.jameswiseman.com/blog/2010/07/07/javascript-copy-to-clipboard/#comments</comments>
		<pubDate>Wed, 07 Jul 2010 08:24:05 +0000</pubDate>
		<dc:creator>jameswiseman</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Clipboard]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Rowspan]]></category>

		<guid isPermaLink="false">http://www.jameswiseman.com/blog/?p=1013</guid>
		<description><![CDATA[I received an enquiry today about a requirement to copy text to the clipboard from JavaScript. The customer seemingly wanted to press a button on their browser to have the data ready in their clipboard for pasting elsewhere. It seems that this is indeed possible, but only after what I would say is an unashamed [...]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p>I received an enquiry today about a requirement to copy text to the clipboard from JavaScript. The customer seemingly wanted to press a button on their browser to have the data ready in their clipboard for pasting elsewhere.</p>
<p>It seems that this is indeed possible, but only after what I would say is an unashamed bodge using Macromedia Flash.<a href="http://www.jameswiseman.com/blog/wp-content/uploads/2010/07/clipboard.jpg"><img class="alignright size-medium wp-image-1036" title="clipboard" src="http://www.jameswiseman.com/blog/wp-content/uploads/2010/07/clipboard-300x300.jpg" alt="Clipboard" width="300" height="300" /></a></p>
<p>The sites detailing the solution for are sufficiently numerous to preclude me from replicating the code here. Some of which include:</p>
<ul>
<li><a href="http://www.logiclabz.com/javascript/copy-to-clipboard-with-javascript-on-mozilla-firefox-and-ie.aspx" target="_blank">http://www.logiclabz.com/javascript/copy-to-clipboard-with-javascript-on-mozilla-firefox-and-ie.aspx</a></li>
<li><a href="http://www.dynamic-tools.net/toolbox/copyToClipboard/" target="_blank">http://www.dynamic-tools.net/toolbox/copyToClipboard/</a></li>
</ul>
<p>In a nutshell, the solution is as follows:</p>
<ol>
<li>Perform some <a title="Object Detection" href="http://www.quirksmode.org/js/support.html" target="_blank">Object Detection</a> for the clipboard object. If it exists, we can use it. If it doesn&#8217;t we go to 2. Generally, the browsers that are more fastidious about security will block access to the clipboard. In years to come I expect this to be all new browsers.</li>
<li> Use an embedded flash object to <del>monkey around with</del> manipulate the clipboard in an <del>alarming</del> clever way.</li>
</ol>
<p><a href="http://www.jameswiseman.com/blog/wp-content/uploads/2010/07/flash10-ed_1.jpg"><img class="alignleft size-full wp-image-1038" title="flash10 ed_1" src="http://www.jameswiseman.com/blog/wp-content/uploads/2010/07/flash10-ed_1.jpg" alt="Flash 10" width="150" height="150" /></a>As it turns out, most of the solutions around will not actually work in Flash 10, where you can only copy to the clipboard if the action originates from user interaction with another Flash object. (<a rel="nofollow" href="http://www.adobe.com/devnet/flashplayer/articles/fplayer10_security_changes_02.html#head31">Read related section from Adobe&#8217;s Flash 10 announcement</a>)</p>
<p>Flash 10 seems to have a number of changes that <a href="http://ajaxian.com/archives/flash-10-and-the-bad-news-for-javascript-interaction" target="_blank">cut down on workarounds for some JavaScript limitations</a>.</p>
<p><a title="Zero Clipboard" href="http://code.google.com/p/zeroclipboard/" target="_blank">Zero Clipboard</a> is reported to circumvent the restrictions. Here is the explanation for this, taken from <a href="http://stackoverflow.com/questions/400212/how-to-copy-to-clipboard-in-javascript" target="_blank">this StackOverflow Post</a>:</p>
<blockquote><p><a title="Zero Clipboard" href="http://code.google.com/p/zeroclipboard/" target="_blank">Zero Clipboard</a> is currently the only library that tries to solve this, by overlaying a Flash object above the Copy button, or whatever element initiates the copy. The current problem with Zero Clipboard is that it uses absolute positioning on the viewport, which breaks when the page is resized. It would be more natural if Zero Clipboard did a 100% height/width overlay inside the wrapper that caught the event.</p></blockquote>
<p>Got that? In short, it all sounds a bit unreliable to me.</p>
<p>So there it is. Anyone straying here hoping to find out how to do a copy WITHOUT flash can expect to be sorely disappointed. And, to be honest, I&#8217;d be nervous about it anyway; who&#8217;s to say that a less-than-reputable website wouldn&#8217;t put down JavaScript to silently copy the contents of your clipboard into an HTTP request periodically. I&#8217;m pretty sure a number of people copy/paste passwords from some stored text-files on their PC.</p>
<p>So what was my solution to the problem? Well, as it turned out, the client actually wanted to copy from a few simultaneous cells in row on an HTML table. When you try to highlight these cells, cells in adjacent rows become highlighted. Give it a try in the hastily-constructed table below:</p>
<table>
<tbody>
<tr>
<td>Data1</td>
<td></td>
<td>Address Line 1</td>
</tr>
<tr>
<td>Data2</td>
<td></td>
<td>Address Line 2</td>
</tr>
<tr>
<td>Data3</td>
<td></td>
<td>Address Line 3</td>
</tr>
</tbody>
</table>
<p>The client was wanting to highlight the three address lines and copy them. Actually performing the copy/paste using Control-C and Control-V was the recognised user experience, and putting a &#8216;Copy&#8217; button on the page would have removed that.</p>
<p>We got around it by embedding a table within a cell and using the trusty old <a title="HTML Colspan Attribute" href="http://www.htmlcodetutorial.com/tables/_TD_COLSPAN.html" target="_blank">ROWSPAN attribute</a> on the cell. Try it now:</p>
<table>
<tbody>
<tr>
<td>Data1</td>
<td></td>
<td rowspan="3">
<table>
<tbody>
<tr>
<td>Address Line 1</td>
</tr>
<tr>
<td>Address Line 2</td>
</tr>
<tr>
<td>Address Line 3</td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<td>Data2</td>
<td></td>
</tr>
<tr>
<td>Data3</td>
<td></td>
</tr>
</tbody>
</table>
<p>It is, of course, slightly misaligned, but a few hasty styling fixes should address this.</p>
<p>If you&#8217;re interested here is the noddy HTML:</p>
<pre name="code" class="html">
<table>
<tbody>
<tr>
<td>Data1</td>
<td></td>
<td rowspan="3">
<table>
<tbody>
<tr>
<td>Address Line 1</td>
</tr>
<tr>
<td>Address Line 2</td>
</tr>
<tr>
<td>Address Line 3</td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<td>Data2</td>
<td></td>
</tr>
<tr>
<td>Data3</td>
<td></td>
</tr>
</tbody>
</table>
</pre>
<p>And finally, the irony of the fact that there is a &#8216;Copy To Clipboard&#8217; option on the syntax highlighted code above is not lost to me!</p>
<div class="shr-publisher-1013"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:none;height:30px;'><a class='shareaholic-fblike' data-shr_layout='button_count' data-shr_showfaces='false' data-shr_href='http%3A%2F%2Fwww.jameswiseman.com%2Fblog%2F2010%2F07%2F07%2Fjavascript-copy-to-clipboard%2F' data-shr_title='JavaScript+Copy+to+Clipboard'></a><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fwww.jameswiseman.com%2Fblog%2F2010%2F07%2F07%2Fjavascript-copy-to-clipboard%2F' data-shr_title='JavaScript+Copy+to+Clipboard'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://www.jameswiseman.com/blog/2010/07/07/javascript-copy-to-clipboard/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	<img style='margin:0;padding:0;border:0;' width='1px' height='1px' src="http://www.jameswiseman.com/blog/wp-content/plugins/mystat/mystat.php?act=time_load&id=616977&rnd=1825663109" /></channel>
</rss>

