views:

199

answers:

5

Hi,

There are many tables in my HTML page. When a user hovers on a table, it should be automatically selected(onmouseover event) so that the user can copy(Ctrl+v) it to clipboard. I searched for a way in stackoverflow and ended up with the following code. But it only works in Firefox (window.getSelection() doesn't work in IE). How can I make it work in IE?

  var prevRange = null;

  function s(node) {
    var s = window.getSelection();
    var r = document.createRange();
    r.selectNode(node);
    if (prevRange) {
      s.removeRange(prevRange);
    }
    s.addRange(r)
    prevRange = r;
  }

For some unknown reason, I can't use s.removeAllRanges() in FF. It gives "invalid label" error. hmmm.

Also, is there a way to programmatically copy the selected code to clipboard?

Thanks.

Sam

A: 

It might be worth your while using a library like jQuery to help deal with browser quirks. It's simple enough to use if you know basic javascript.

http://jquery.com/

psychotik
A: 

This is only to show which method to use when in IE and when using other browsers, it's not a complete solution by far and contains at least one logic trap.

var s;
if (window.getSelection) {
    s = window.getSelection();
}
else if (document.selection) {
    s = document.selection.createRange();
}

s should be a text range in IE, while it remains a selection-object in Mozilla-based browsers.

Information found here: http://www.quirksmode.org/dom/range%5Fintro.html

Edit: Edited for clarification. =)

J. Steen
The two things are not equivalent. In IE, s is a TextRange, which is equivalent to a Range in other browsers, while for non-IE s is a selection object.
Tim Down
Which is what I wrote in the answer. It's a HELP, not a complete solution. =)
J. Steen
@Tim Down, I will most definitely say that your answer holds a much more complete listing, however. =)
J. Steen
OK, I missed the last bit. Still, it doesn't seem terribly helpful to have non-equivalent objects stored in the same variable depending on the browser.
Tim Down
@Tim Down, No, you're rather right about the nonhelpfulness of the variable being the same in both cases, however it was meant to show which METHOD to use, when in IE. Nothing more. =)
J. Steen
A: 

This is a nice article explaining range differences between most common browsers and gives you useful tips to overcome them.

RaYell
A: 

Regarding getSelection this has been answered by J. Steen,

var s;
if (window.getSelection) {
    s = window.getSelection();
}
else if (document.selection) {
    s = document.selection.createRange();
}

Regarding programmatically copy the selected text to clipboard, this is not working in Firefox due to security reasons. There are some workaround but the end user needs to change some settings. Java applets can be used to copy text in Firefox programmatically (java applets is not a nice solution however).

Per Östlund
+1  A: 

The following function will do what you want. No need for jQuery.

I've seen no problem with removeAllRanges in Firefox. You need to provide more code to illustrate the problem.

You won't be able to copy the user selection directly to the clipboard, as noted in another answer. Most browsers rightly don't allow it.

function selectElement(el) {
    var sel, range;

    // IE branch
    if (document.body.createTextRange) {
     range = document.body.createTextRange();
     range.moveToElementText(el);
     range.select();
    } 

    // DOM compliant browsers
    else if (window.getSelection && document.createRange) {
     sel = window.getSelection();
     range = document.createRange();
     range.selectNodeContents(el);
     sel.removeAllRanges();
     sel.addRange(range);
    }
}
Tim Down