all groups > dotnet jscript > february 2005 >
You're in the

dotnet jscript

group:

Can't Copy Data To Clipboard


Can't Copy Data To Clipboard Wayne Wengert
2/23/2005 9:42:07 AM
dotnet jscript:
I am trying to copy the text from a textbox to the clipboard. I saw some
code posted earlier for Pasting from the clipboard so I modified it for copy
but it is not working (see code below). When I click on the
btnCopyToClipboard I can see some processing going on but the text from the
textbox is not being put on the clipboard? I tried changing the
"txtHidden.focus()" to "innerHTML" but that also gave no data on the
clipboard?

Thoughts or suggestions?

Wayne


====================================
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

If Not IsPostBack Then

btnCopyToClipboard.Attributes.Add("onclick", "CopyIt();return false;")

Page.RegisterClientScriptBlock("copyroutine", _

"<script>function CopyIt()" & _

"{document.all.txtHidden.focus();" & _

"document.execCommand('copy');}</script>")

End If

End Sub

Re: Can't Copy Data To Clipboard Serge Baltic
2/24/2005 2:08:48 AM
WW> I am trying to copy the text from a textbox to the clipboard. I saw
WW> some code posted earlier for Pasting from the clipboard so I
WW> modified it for copy but it is not working (see code below).W>

Just a question, are you sure that it's not prohibited by security settings
of the target machine? That could easily be the cause.

--
Serge

Re: Can't Copy Data To Clipboard Wayne Wengert
2/24/2005 5:45:27 AM
Thanks for the suggestion but this is on my development system which does
accept copy from other web sites so I don't think that is the issue.

Wayne

[quoted text, click to view]

Re: Can't Copy Data To Clipboard Serge Baltic
2/24/2005 8:26:43 AM
WW> Thanks for the suggestion but this is on my development system which
WW> does accept copy from other web sites so I don't think that is the
WW> issue.

OKAY, then you could try this:

document.getElementById("txtHidden").createTextRange().execCommand(....)

What about that?

Execing the command on document/body does apply to the current text selection,
which has little to do to your hidden element …

--
Serge

Re: Can't Copy Data To Clipboard Wayne Wengert
2/24/2005 10:49:25 AM
Serge;

Again, thanks for responding.

You are saying that the
"document.getElementById("txtHidden").createTextRange().execCommand(....)"
will select the text in that text box? Excuse my dumb questions but the
workings of javascript really have me lost!

Wayne

[quoted text, click to view]

Re: Can't Copy Data To Clipboard Serge Baltic
2/24/2005 11:40:27 AM
WW> You are saying that the
WW> "document.getElementById("txtHidden").createTextRange().execCommand(....)"
will select the text in that text box?

Not quite. I'm not sure for the textbox at all (have never checked that),
but for the ordinary text that should definitely do the thing.

"document.getElementById("txtHidden")" is just a slightly better approach
to getting the element than "document.all.txtHidden", that does not make
sense. "createTextRange" gives an object representing a piece of text which
falls inside the element you are calling this function on. This text does
not get selected, but it can be manipulated with. For example, you can retrieve
this text as plaintext or HTML, or really select it, or execute any of the
browser commands (see the IHTMLTxtRange interface in MSDN, or Text range
object, for details). In your case, you would like to execute the copy-to-clipboard
command, but not on the whole document (which has no selection and thus won't
complete your request), and without selecting anything, but just applying
"copy" to the chosen range.

Well, here's a working sample for you, enjoy:

<html>
<input id="txtHidden" type="text" value="Hello, World!" style="display: none;"
/>
<button onclick='document.getElementById("txtHidden").createTextRange().execCommand("copy")'>Copy</button>
</html>

Tat's it. Just have no idea of how this goes with browsers other than ones
based on MSHTML ;-)

--
Serge

Re: Can't Copy Data To Clipboard Wayne Wengert
2/24/2005 3:42:06 PM
Thanks Serge;

I'll try applying that to my case.

Wayne

[quoted text, click to view]
onclick='document.getElementById("txtHidden").createTextRange().execCommand(
"copy")'>Copy</button>
[quoted text, click to view]

Re: Can't Copy Data To Clipboard Wayne Wengert
2/24/2005 3:56:59 PM
Serge;

I am back to the original problem - txtHidden is a textbox in an ASP.NET
page. I cannot use the old <input..> tag. The problem seems to be how to
select the text in an ASP.Net textbox in javascript. I'll do some more
google searches to see if I can figure this out.

Wayne

[quoted text, click to view]
onclick='document.getElementById("txtHidden").createTextRange().execCommand(
"copy")'>Copy</button>
[quoted text, click to view]

Re: Can't Copy Data To Clipboard Serge Baltic
2/25/2005 1:23:24 AM
WW> I am back to the original problem - txtHidden is a textbox in an
WW> ASP.NET page. I cannot use the old <input..> tag. The problem seems
WW> to be how to select the text in an ASP.Net textbox in javascript.
WW> I'll do some more google searches to see if I can figure this out.

Which exactly HTML tag is it using? I guess it's the same good old <input/>
when the page is delivered to client machine, no? What does "View Source"
say?

--
Serg

AddThis Social Bookmark Button