I'd like to put a button on a page. When clicking the button, the table below it gets selected so the user can do Ctrl C to copy the entire table without using the mouse to select the table which can be big. How do I do it using javascript? I tried:
> I'd like to put a button on a page. When clicking the button, the > table below it gets selected so the user can do Ctrl C to copy the > entire table without using the mouse to select the table which can be > big. How do I do it using javascript? I tried:
Botao wrote: > I'd like to put a button on a page. When clicking the button, the > table below it gets selected so the user can do Ctrl C to copy the > entire table without using the mouse to select the table which can be > big. How do I do it using javascript?
You'll be able to do the selection by using so-called 'ranges,' which are so far supported by IE4+ and Mozilla. The copy command can also be performed, in IE by using execCommand and in Mozilla by using clipboard helpers (which however require higher security privileges).
> I got error "document.MyTable is null or not an object".
That's because the way you try to get a reference to the table object isn't correct, give a look at
<table id="trainOfThought"> <tr><td>As I Am</td></tr> <tr><td>This Dying Soul</td></tr> <tr><td>Endless Sacrifice</td></tr> <tr><td>Honor Thy Father</td></tr> <tr><td>Vacant</td></tr> <tr><td>Stream Of Consciousness</td></tr> <tr><td>In The Name Of God</td></tr> </table>
<script type="text/javascript"> var Utils = { NOT_SUPPORTED : {},
Botao wrote: > Thanks much for your reply. Too bad the "range" is only supported in > IE4. So I guess there is no way in IE5 to do "selecting" using > javascript.
There's a misunderstanding, Botao. I've written IE4+, which means *from* IE4 onwards, so the script should also work on IE5, IE5.5 and IE6:-)
To test it by yourself, create a blank HTML file, copy-paste the code I've provided, and run the HTML page into your favorite browser. If the command button is visible, then this means that the script is supported (even though, from a technical point of view, I could have been a bit more precise when deciding whether to write the button).
> Thanks much for your reply. Too bad the "range" is only supported in > IE4. So I guess there is no way in IE5 to do "selecting" using > javascript.
If your concern is Windows IE, you can replace that entire pile of obfuscating syntax with two lines: var oControlRange = document.body.createTextRange(); oControlRange.moveToElementText(obj);
Look up moveToElementText. All you do is give it an object
reference to the table in the form of document.getElementById("tableID")
Could I ask one more thing? How could I on top of you code, add copy command to copy the selection. Then open an word or excel, paste the selection to a excel or work doc?
Botao wrote: > Could I ask one more thing? How could I on top of you code, add copy > command to copy the selection. Then open an word or excel, paste the > selection to a excel or work doc?
In a normal security environment this is not possible, and you haven't stated in which environment you're working. I suspect you're intending the code for an IE-based intranet, though.
There can be a big difference between Internet scripting and Intranet scripting. When designing for the Web, you have to take into account that the runtime environment cannot be known for sure (there are more than 100 browsers available), and that the script will fail inevitably on some user agents. You therefore need to use a very defensive coding approach, so that the script works fine on supporting platforms, and fails silently on others (concept of clean degradation) - the original script was coded in this perspective, which added a complexity overhead (all the more the ranges' models differ greatly between IE's and W3C's).
On the other hand, coding for an intranet enables simpler approaches and extended functionalities, since: - you know the user agent, - you know that javascript is enabled, - you know that the security settings are high enough to use external components (namely for IE, ActiveX).
This can simplify things greatly; see below (script intended for IE5+ with high security settings).
> I'd like to put a button on a page. When clicking the button, the > table below it gets selected so the user can do Ctrl C to copy the > entire table without using the mouse to select the table which can be > big. How do I do it using javascript? I tried: