Select a code sample – using JavaScript
February 26th, 2005
function turnon(td) {
td.className='divmenuover';
}
function turnoff(td) {
td.className='divmenu';
}
function copycode(ta) {
var obj = document.forms[ta].code;
if ( (navigator.appName=="Microsoft Internet Explorer") && (parseInt(navigator.appVersion)>=4) ) {
copythecode(obj);
}
else {
highlightthecode(obj);
}
}
function highlightthecode(obj) {
obj.select();
obj.focus();
}
function copythecode(obj) {
highlightthecode(obj);
textRange = obj.createTextRange();
textRange.execCommand("RemoveFormat");
textRange.execCommand("Copy");
alert("Code segment has been copied to your clipboard.");
}
The following code (from www.searchengineforums.com) allows you to click a link to select all the code in a textarea box. You can then Ctrl+C it to copy it. If you're using Internet Explorer, it's copied to the clipboard for you.
As I mentioned above, this piece of Javascript comes from www.searchengineforums.com. This site contains a stack of interesting forums and is well worth checking out.
Potentially similar posts
- Convert escaped Unicode to HTML entities – January 2012
- My first (useful) Ruby program – June 2011
- Viewing dynamically generated HTML in the HTML Help viewer – November 2010
- Use the existence of a file on the server to determine Javascript behaviour in the browser – November 2010
- Preventing BBC iPlayer running all the time – November 2008