Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Copy to clipboard - newline

Status
Not open for further replies.

IlseDC

Programmer
Mar 24, 2003
49
0
0
BE
I want to copy a string to the clipboard.
The string must have some newlines (CRLF).
When I run the page I get an error.

This is the code:

<SCRIPT LANGUAGE='JavaScript'>
function CopyToClipboard()
{
var Data;
Data = 'This is the first line.' + '\n' + 'This is the second line.';
window.clipboardData.setData('TEXT', Data);
}
</SCRIPT>


The source of the html page is:

<SCRIPT LANGUAGE='JavaScript'>
function CopyToClipboard()
{
var Data;
Data = 'This is the first line.' + '
' + 'This is the second line.';
window.clipboardData.setData('TEXT', Data);
}
</SCRIPT>

So the '\n' is translated into a newline and the javascript does not work.

What can I do so that the javascript works and the text to copy to the clipboard can have some newlines?

Thanks.


 
I make the javascript code at server side.
At server side I make a string strScript with the javascript code and I do Page.RegisterClientScriptBlock("CopyToClipboardRoutine", strScript).

The content of strScript is:
"<SCRIPT LANGUAGE='JavaScript'>function CopyToClipboard(){ var Data;Data = 'This is the first line.' + '\n' + 'This is the second line.';window.clipboardData.setData('TEXT',Data);}</SCRIPT>"
 
If you have your script server-side, then "\n" will come through to the client-side as a new line. This is not a bug.

However, if you want to run your script client-side, then don't go putting it server-side. Put it client-side.

Dan


[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
>What can I do so that the javascript works and the text to copy to the clipboard can have some newlines?
As long as it is client-side and ie as user agent, \n produces the newline. I don't see the conflict.
Server-side?...what for.
 
I need to use server side and RegisterClientScriptBlock because in that javascript function I need the content of a textbox, for example txtTest.
When I define the script at client side I tried to use document.getElementById to find the textbox and so retrieve the content. But getElementById doesn't work because I don't know the exact name of the textbox.
The name at run time is not txtTest but xxxxx_txtTest (xxxxx = combination of letters and numbers).
So getElementById does not work and I can not retrieve the content of the textbox.
 
Textbox? What textbox? I see no textbox here.

Maybe you should take a step back, sit down, and work out what your problem is, and think how best to divide the server-side and client-side code.

It sounds like you don't know the architecture of your app - and if that is the case, i'm not sure posting here will help you any further.

Hope this helps,
Dan


[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
I do know the structure of my application.

The first problem was that I had to copy the content of a textbox to the clipboard. I did this with a javascript function that I defined at server side with RegisterClientScriptBlock.
In the content of that textbox there were newlines so the javascript gave an error at run time because of the newlines.

So someone suggested to define the javascript function in the html page, so directly at client side instead of server side. Then there are no problem with newline.
In that javascript function I use getElementById to retrieve the content of the textbox. But at run time the control has a different name than the one I used in the source of my program. So getElementById does not find the textbox.

In the example I gave I use "Data" to copy to the clipboard but in my app this has to be the content of the textbox.
 
self quoting
>Server-side?...what for.
I meant copy anything at all to the clipboard of the server on the server. But, it is fair enough you have different understanding. So, leave that aspect out.

If you mean anything of the textbox's id being generated dynamically at _server-side_, then this illustrate how you tackle the problem. I use a simple asp page. (But then you might come back by saying you have jsp/php or whatever. The idea is the same.)
[tt]
<% @language="jscript" %>
<%
//is this dynamic enough?
var id1="id_"+Math.floor(Math.random()*100000);
var id2="id_"+Math.floor(Math.random()*10000000);
var line1=Math.floor(Math.random()*1000000000);
var line2=Math.floor(Math.random()*1000000000000);
%>
<html>
<head>
<script language="javascript">
function CopyToClipboard() {
var Data;
//Data = 'This is the first line.' + '\n' + 'This is the second line.';
Data = document.getElementById("<%=id1%>").value+"\n"+document.getElementById("<%=id2%>").value;
window.clipboardData.setData('TEXT', Data);
}
</script>
</head>
<body>
<!-- just for checking -->
<% Response.write (id1 + "<br />"); %>
<% Response.write (id2 + "<br />"); %>
<br />
<button onclick="CopyToClipboard()">do the copy to clipboard</button>
<form action="">
<input type="text" id="<%=id1%>" value="<%=line1%>"><br />
<input type="text" id="<%=id2%>" value="<%=line2%>"><br />
</form>
</body>
</html>
[/tt]
If you have asp server available, you can test it to see for yourself.

Now, as to what your script function on the client-side being rendered in two lines like you shown in the second excerpt, then no doubt you write your server side script wrong---that's all. Why imagine or doubt client-side javascript conflict with copy to clipboard with newline using ie's proprietary method?

Your script sector must be written using asp-equivalent Response.write method. You have to look into it if you need to escape you "\" by doubling it up or something.
 
Thanks for your answer.
The getElementById("<%=id1%>") works fine.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top