clip board data as html

S

SirCodesALot

hi all,

I am trying to copy the contents of a div to the clip board the same
way that Microsoft copies it when you select the contents with the
mouse and select copy. However, something is not the same.

here is the code simplified:
var s = document.getElementById("testDiv").innerHTML;
if( window.clipboardData && clipboardData.setData )
{
clipboardData.setData("text", s);
}

If I use this method to copy to clipboard, then to Word, i get:
<TABLE><TBODY><TR><TD>text</TD></TR></TBODY></TABLE>

however, if I select the table with the mouse, select copy, and paste
in to word, the actual table shows up like it did in the web page.

Any way to make the clipboard put the html in the correct format that
word wants?

Thanks,
-Scott
 
E

Evertjan.

SirCodesALot wrote on 30 mei 2008 in comp.lang.javascript:
hi all,

I am trying to copy the contents of a div to the clip board the same
way that Microsoft copies it when you select the contents with the
mouse and select copy. However, something is not the same.

here is the code simplified:
var s = document.getElementById("testDiv").innerHTML;

Try IE only:

var s = document.getElementById("testDiv").innerText
 
S

SirCodesALot

SirCodesALot wrote on 30 mei 2008 in comp.lang.javascript:




Try IE only:

var s = document.getElementById("testDiv").innerText










--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)- Hide quoted text -

- Show quoted text -

thanks for your reply, that does work, but it drops all the formating
for the tabels. When you selected with content with the mouse and do a
copy, MS must be saving other information about the formating for the
table.
 
S

SirCodesALot

thanks for your reply, that does work, but it drops all the formating
for the tabels. When you selected with content with the mouse and do a
copy, MS must be saving other information about the formating for the
table.- Hide quoted text -

- Show quoted text -

I found a way to do it in IE:
function copyDivToClipboard(divId) {
var range = document.createRange();
range.selectNode(document.getElementById(divId));
window.getSelection().removeAllRanges();
window.getSelection().addRange(range);

try
{
range.execCommand("Copy");
}
catch (e)
{
alert("Sorry, your browser does not support this feature. Please
right click the highlighted text and select 'copy'.")
}
}
 
H

Holger Jeromin

SirCodesALot schrieb am 30.05.2008 23:11:
thanks for your reply, that does work, but it drops all the formating
for the tabels. When you selected with content with the mouse and do a
copy, MS must be saving other information about the formating for the
table.

The Clipboard in Windows is capable to host multiple information
simultanously.

Text only+HTML coded version

A Texteditor used the textonly, while Word is able to recognise the HTML
design.

Try in Word "edit/paste special..." to see what is in there.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top