|
From: Álvaro G. Vicario on 24 Apr 2008 06:01 I've written this function to select text from JavaScript in a web page: /* * Selects node's text * * Usage: <input type="button" value="Select" * onclick="selectNode(document.getElementById('foo'))"> */ function selectNode(node){ // v2008-04-23 // Create a range try{ var range = document.createRange(); }catch(e){ try{ var range = document.body.createTextRange(); }catch(e){ return; } } // Asign text to range try{ range.selectNode(node); }catch(e){ try{ range.moveToElementText(node); }catch(e){ return; } } // Select range try{ var seleccion = window.getSelection(); seleccion.addRange(range); }catch(e){ try{ range.select(); }catch(e){ return; } } } I use this script to select data tables double clicking on them (so I can then copy and paste into a spreadsheet or whatever) and it works in both IE and Firefox (not Opera or Konqueror). However, it has a weird issue in Firefox. Most of the times (but not always) there's an extra cell at the end of the table, with random contents taken from the table (but not always the same). Firebug's DOM viewer doesn't show that extra cell but the cell gets there as soon as I copy the selection into clipboard. I've seen this behaviour in Firefox 2 and Firefox 3 beta 5. It also happens in Windows XP, Windows Vista and Fedora 8. It's all random but easy to reproduce. Have I made any obvious mistake in my code? -- -- http://alvaro.es - �lvaro G. Vicario - Burgos, Spain -- Mi sitio sobre programaci�n web: http://bits.demogracia.com -- Mi web de humor al ba�o Mar�a: http://www.demogracia.com --
|
Pages: 1 Prev: javascript to access registry keys of Internet explorer Next: CSS Question |