views:

653

answers:

6

One of the things I'd like to do in my browser-based application is allow the user to select some text (not in a <textarea>, just plain ol' text!), and have my application pop up a small toolbar that then can interact with the next (in my case, add annotations).

I've found a lot of stuff on google that seems to be focused on writing WYSIWYG editors, but that isn't what I want, and most of it worked in IE but not in FF2 or 3. Ideally, I'd like some function that can return the currently selected text in the browser window that works in IE7 (and 6 if possible), FireFox 2 & 3 and Safari 2. If it works in Opera, that'd be a bonus, but it's not a requirement.

Anyone have a function that does this? Or an idea of where to start?

+4  A: 

Have a look at jQuery and the wrapSelection plugin. It may be what you are looking for.

palehorse
A: 

Wow, that's freaking awesome. It doesn't work in Safari, unfortunately, but I'd be willing to sacrifice that for now.

Thanks!

Tim Sullivan
Please, this is not a forum, only Answers goes here
vsync
vsync: this answer predates the commenting feature on Stack Overflow.
Tim Sullivan
A: 

Introduction to Range has some details on how different browsers give you access to the text selection.

My experience is that working with these different APIs directly is quite clumsy so if wrapSelection works for you I'd go with that.

wrumsby
A: 

The behaviour of individual browsers with regard to selection is outlined here.

eplawless
A: 

Wow, that's freaking awesome.

You wouldn't care to enlighten us as to what you're replying to, would you? Answers have these nifty link things, or you can quote using angle brackets for maximum clarity. So I'm not sure what stage you're at or if this duplicates what you already have.

This code works in Safari, IE and Firefox - hope it's of some help

var str = (window.getSelection) ? window.getSelection() : document.selection.createRange();
str = str.text || str;
str = str + ''; // the best way to make object a string...
Flubba
+2  A: 

That jQuery plugin is cool but it accomplishes a very specific task: wrap the text you highlight with a tag. This may be just what you want. But if you don't want to (or are in a situation where you can't) add any extraneous markup to your page, you might try the following solution instead:

function getSelectedText() {
  var txt = '';
  if (window.getSelection) {
    txt = window.getSelection();
  }
  else if (document.getSelection) {
    txt = document.getSelection();
  }
  else if (document.selection) {
    txt = document.selection.createRange().text;
  }
  else return; 
  return txt;
}

This function returns an object representing the text selection. It works across browsers (though I suspect the objects it returns will be slightly different depending on the browser and only dependable for the actual text of the result rather than any of the additional properties).

Note: I originally discovered that code fragment here: http://www.codetoad.com/javascript_get_selected_text.asp

Greg Borenstein