Copy a table on a Web page from a JavaScript script

Source: Internet
Author: User
Tags continue copy
javascript| Script | Web page

<input type= "button" value= "Copy Test Form" >
<input type= "button" value= "Output the Clipboard contents to the editor" ><BR>
Test
<table border= "1" id= "otable" >
<TR>
<TD> Test Form </TD>
<TD> Test Form </TD>
</TR>
<TR>
<TD> Test Form </TD>
<TD> Test Form </TD>
</TR>
</TABLE> text <BR><BR>
<iframe id= "editor" src= "About:blank" ></iframe>
<script language= "JavaScript" >
<!--
function copytable ()
{
Copyhtmlelement (otable)
}

function copyhtmlelement (obj)
{
Editor.document.designMode = ' on '; Turning an IFRAME into an editable mode, the HTML editor
Editor.document.write ("<body></body>"); Initialize Editor
Editor.document.body.innerHTML = obj.outerhtml;
Editor.document.body.createTextRange (). Select (); Select all contents in the editor
Editor.document.execCommand ("Copy", "", null); Copy
}
function Pastclipboarddata ()
{
Editor.focus ();
Editor.document.execCommand ("Paste", "", null); Paste
}
-->
</SCRIPT>

This function is implemented, but feel a bit uncomfortable, you must use the HTML editor, but from the full selection of HTML editor in the code, notice the createTextRange () method, this method has been useful before, but has not been thoroughly studied. Found in MSDN that only objects such as body, textarea, and so on support the createTextRange () method, continue to be looked up in MSDN, createTextRange () returns a TextRange object, continues to consult TextRange object, found that there are many methods, first tried FindText method, found only text, can not select the object, continue to find, finally found Movetoelementtext is the way we are looking for:


<input type= "button" value= "Checked Test form" >
Test
<table border= "1" id= "otable" >
<TR>
<TD> Test Form </TD>
<TD> Test Form </TD>
</TR>
<TR>
<TD> Test Form </TD>
<TD> Test Form </TD>
</TR>
</TABLE> text
<script language= "JavaScript" >
<!--
function copytable ()
{
var txt = document.body.createTextRange ();
Txt.movetoelementtext (otable);
Txt.select ();
}
-->
</SCRIPT>



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.