PDA

View Full Version : Copy To Clipboard Button



yerajeff
30 Jul 2010, 9:53 AM
After searching the web and forums for a copy to clipboard solution, I came up with my own. For my application I needed a button in my tbar that could copy my specific string to the clipboard. IE offers a function to do this, but for other browsers I created a flash file that replaces the IE code and passes the variable to be copied directly to flash (flash then takes care of the clipboard copy).

My application is a combination of PHP and EXTJS. I made my browser detections using PHP, but it can be done other ways. This is the code for my tbar:



//replace 'String To Be Copied' with your text or your concatenated variable: '+textString+'
tbar: [
{
<? if($ua['msie']) { ?>
text: 'Copy Code',
tooltip: 'Copy the code',
iconCls:'page-code',
handler: function(){
if( window.clipboardData && window.clipboardData.setData ) {
window.clipboardData.setData("Text", 'String To Be Copied');
}
}
<? } else { ?>
xtype: 'panel',
width: 79,
height: 22,
bodyStyle: 'background-color:transparent; border:none;',
border: false,
html: '<embed id="flashid" src="ClipboardCopy.swf?varName=String To Be Copied" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="79" height="22" name="flashid" align="middle" allowScriptAccess="always" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" wmode="transparent" />'
<? } ?>
}
]

Download the fla here: http://www.4shared.com/file/WltjuceG/ClipboardCopy.html
so you can change the button design and see how the actionscript works, etc... All you need is the code above and the swf in the same directory (or modify the above code for whatever directory...).

And if you are using PHP, here is the script I used for browser detection:


function browser_info($agent=null) {
// Declare known browsers to look for
$known = array('msie', 'firefox', 'safari', 'webkit', 'opera', 'netscape',
'konqueror', 'gecko');

// Clean up agent and build regex that matches phrases for known browsers
// (e.g. "Firefox/2.0" or "MSIE 6.0" (This only matches the major and minor
// version numbers. E.g. "2.0.0.6" is parsed as simply "2.0"
$agent = strtolower($agent ? $agent : $_SERVER['HTTP_USER_AGENT']);
$pattern = '#(?<browser>' . join('|', $known) .
')[/ ]+(?<version>[0-9]+(?:\.[0-9]+)?)#';

// Find all phrases (or return empty array if none found)
if (!preg_match_all($pattern, $agent, $matches)) return array();

// Since some UAs have more than one phrase (e.g Firefox has a Gecko phrase,
// Opera 7,8 have a MSIE phrase), use the last one found (the right-most one
// in the UA). That's usually the most correct.
$i = count($matches['browser'])-1;
return array($matches['browser'][$i] => $matches['version'][$i]);
}

$ua = browser_info();
/*
if ($ua['firefox']) ... // true
if ($ua['firefox'] > 3) ... // true
if ($ua['firefox'] > 4) ... // false
if ($ua['browser'] == 'firefox') ... // true
if ($ua['version'] > 3.5) ... // true
if ($ua['msie']) ... // false ('msie' key not defined)
if ($ua['opera'] > 3) ... // false ('opera' key not defined)
if ($ua['safari'] < 3) ... // false also ('safari' key not defined)
*/


Feel free to use, modify, redistribute or do whatever you want with this contribution.