Skip Navigation

About This Website

CodeStore is all about web development. Mostly with the Lotus Domino server.

Your host is Jake Howlett who runs his own web development company called Rockall Design and is always on the lookout for new and interesting work to do.

You can find me on Twitter and on Linked In.

Read more about this site »

About This Article

Author: Jake Howlett
Date: 26 October 2000
Article: DOMM-4QHQE8
Category: JavaScript
Keywords: copy; range; select;

Options

Feedback
Print Friendly

Copying text from a document to the clipboard

This is another one of those things that you will either love or hate. I love it ;-)

It only works in Internet Explorer on 32 bit Windows platforms, so is only really useful for things like Intranets, where you can be totally sure of the user's environment.

This is the script needed:

function copyText( obj ) {

/*create textRange differently
depending what the object is */
if (obj.type=="text" || obj.type=="textarea"){
var rng = obj.createTextRange();
} else {
var rng = document.body.createTextRange();
rng.moveToElementText(obj);
}

rng.scrollIntoView();
rng.select();

if (confirm('Copy the selected text to the ClipBoard?')) rng.execCommand("Copy");
rng.collapse(false);
rng.select();

}


Now let's test it out. What better way than to copy the above function. You can paste it straight into your script file then.

The script (coloured text) is in a <span> tag with an ID of "testCopy" so we can call the function with a referrence to this span as an "object". Like this:

javascript:copyText(document.all.testCopy);

Here is an box for you to paste in to, in case you don't believe me:



You don't have to use a span for your selection. You can copy text from almost any object. For example:

Copy the first column of the fourth row of a table called "content" - copyText(document.all.content.rows(3).cells(0));

"So what!" you may be saying. "The user can easily drag over that area and copy it theirselves". Yeah, I know, but don't you think this is nice !? I'm sure you could think of a use for it somewhere...

If you want to take this idea further I suggest you get "Dynamic HTML" by Danny Goodman, listed here, and read Appendix D which has a full list of the available execCommand() methods. Which, by the way, includes "Paste".

Feedback

  1. MSHTML

    You can do much more with the execCommand() methods. It's possible to build an editor with MSHTML, for further information click on the link below. It's not an ordinary DHTML editor. http://msdn.microsoft.com/workshop/browser/mshtml/overview/mshtml_editing_ovw_en try.asp?frame=true

    The editor I built using MSHTML is much better then the editor applet used in Domino.

    1. Re: MSHTML

      More info on how to build an editor using MSHTML. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnmshtml/html/c reatewp.asp?frame=true

Add your own response here:

Although your email address isn't required it is protected from spambots if you choose to provide it and not to hide it. My right to remove commercial, irrelevant or posts I just don't like is reserved.

Name *:
E-mail:
Website:
 

Comment *:

HTML is not allowed!
 

Navigate other articles in the category "JavaScript"

« Previous Article Next Article »
Locating File Upload Controls with JavaScript   Creating dynamic references to objects