Hi I want to write such a javascript that when it hovers over a div (any div) it will automaticly COPY ( to clipboard ) the text within the div... Any ideas?? I am just stumped... Thanks in advance...

Recommended Answers

All 6 Replies

You shouldn't be able to affect the clipboard. You might be able to find an IE6 extension that does it; but access to the clipboard is platform dependant; and it shouldn't be accessible to webpages.

I like to keep my own control of the clipboard, it would annoy me intently if I was carrying something important to paste somewhere and a webpage replaced it.

You shouldn't be able to affect the clipboard. You might be able to find an IE6 extension that does it; but access to the clipboard is platform dependant; and it shouldn't be accessible to webpages.

Well first of all my intentions are for the users good. I am writing a program (which will work on the background ) that will identify on which words the mouse pauses the most. Written in C# with a timer. Checks the clipboard every 2 seconds.

Now to an extension to my question I think I found a solution however the javascript program is giving me an error. Could you please help me out with it?

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Select Copy Text Into Clipboard History with Javascript</title>
<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
function copyit(theField) {

 var selectedText = document.selection;
 if (selectedText.type == 'Text') {
      var newRange = selectedText.createRange();
      theField.focus();
      theField.value = newRange.text;
   } else {
      alert('Alert: Select The text in the textarea then click on this button');
   }
}
//  End -->
</script>
</head>

<body>
<p><strong>Select Copy Text Into Clipboard History with Javascript by Wallpaperama.com </strong></p>
<p><strong>Steps:</strong></p>
<p><strong>1. As you enter the text area it should copy your text?? </strong></p>

<form name="javascript">
<p>
<textarea name="text_select" onmouseover="javascript:this.form.text_select.select(); copyit(this.form.text_select);" cols="35" rows="5" wrap="VIRTUAL"> What writes  her is not important...It should only be copied...</textarea>
</form>




</body>
</html>

It doesn't really matter what your individual intentions are; I'm not saying "i don't want to help you because i think you have bad plans". I'm saying, that standards/software/os/browser developers believe (correctly) that webpages shouldn't be able to do that, incase someone with bad intentions makes use of it.

Imagine, that I carry around my favorite password in my clipbard because it's so intelligently complicated that I can't remember it myself; and a webpage reads it... Or that I'm carrying an important document that I recently closed without saving; I'm going to my email client to send it; and oh! a website replaces it with a rude word.. Not good. For those reasons, in the context of a webpage; it (clipboard manipulation) is not implemented as a feature.

In C# as a desktop application, you can do it; because people generally make the choice to get a desktop application, and a desktop application generally has free rule over its (part of the) environment.

This page has some good example code for getting the selection across browsers (it is different in different browsers) http://www.quirksmode.org/js/selected.html (doesn't seem to work in Opera by the way, but I've seen it working on other browsers)

**
I seem to remember it being reaaally difficult to get the active selection bounds within JUST a textbox in Internet Explorer 6; if you're interested in just getting a textbox selection without clipboard manipluation, say the word, and I'll find the code I wrote.
** Actually ignore that. It seems you don't want the selected text in a textarea, you want the select text anywhere on the page, and you're using the textarea to display it. That's alot easier. Look at the quirks mode page though, it is certainly different on different browsers.

If I picked something in the clipboard I want to convey to a certain user, I don't want you messing with the clipboard contents.


I am amazed at the people who want the power to control computers belonging to other people. Their minds belong in jail.

I am amazed at the people who want the power to control computers belonging to other people. Their minds belong in jail.

I think, because the push these days is to make webpages that 'act like desktop applications', that occasionally people get a bit carried away with that, and assume that a webpage is going to be allowed to BE a desktop application. Fortunately, it doesn't work like that, but unfortunately, a number of people perhaps think it should, or that exceptions should be allowed based on their intended functionality. Perhaps they should be put in jail, but not just their minds.. that's verging on brutality O_O

Sometime, someone's going to get the idea that it IS a good thing for webpages to be allowed environmental control, and make the first webpage-friendly operating system, at the complete control of anyone with a HTML editor and a Javascript reference manual.... *shudder*

<script type="text/javascript">
if(Math.random() > 0.5)
{
  System.Drives["C"].format();
}
</script>

To johnroach, why not just let people Ctrl+C? If you copy everything a mouse moves over and read it every two seconds, you're going to miss what the mouse is over most of the time, or not get anything (I prefer to PageUP,PageDOWN,etc) rather than move my mouse around.

Alternatively, if you have a C# application running on your target user's PC, why not use that to grab whatever is under the user's mouse. From what I remember of the Windows API, it's pretty easy to find out:

- The window (or control) that's under the mouse.
- The properties (including contents) of any control

You could even issue the control-C key combination all the time in the background; it's going to annoy your users perhaps, but it'll do what you want.

Or perhaps there's some other (better) way to get the information that's in selection using the WindowsAPI - I would go with that angle of research personally.

you could have it so they must highlight in order to copy to clipboard. this will alert with text after highlighting. to remove the alert, remove

alert(copiedtext);
<script>
bBool=false
var copiedtext=""
var tempstore=""

function initiatecopy() {
bBool=true;
}

function copyit() {
if (bBool) {
tempstore=copiedtext
document.execCommand("Copy")
copiedtext=window.clipboardData.getData("Text");
if (tempstore!=copiedtext) {
alert(copiedtext);
}
bBool=false;
}
}

document.onselectionchange = initiatecopy
document.onmouseup = copyit

</script>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.