tags:

views:

1170

answers:

1

Several of years ago, I added "smart quoting" to a web forum. Basically, user selects a part in previous conversation and clicks a button to quote it. Script gets the HTML of the quote and goes up the DOM tree to figure out who said that.

I could only do it for IE, although I remember trying hard. But then, there was no stackoverflow.com and Firefox was not as mature. I guess that by now, doing it in Firefox is as easy. Here's the key part of the code.

range2Copy = frameDoc.selection.createRange(); 
html2Copy = range2Copy.htmlText; 

el = range2Copy.parentElement();

// go up the HTML tree until post row node (id=postrowNNNN)

while (el.nodeName != 'BODY' &&
     !el.id.match(/postrow/)) {

    el = el.parentNode;
}

Element frameDoc contains the previous thread where user selects text. If it makes too little sense, see the whole code here. It is a plugin for FCKeditor.

+2  A: 

OK, I tried to run your code in firefox and it didn't work, this is the modified version that worked:

var selection = window.getSelection(); 
var node = selection.anchorNode;

while (node.nodeName != 'BODY' && !node.id.match(/postrow/)){
    node = node.parentNode;
}
Nadia Alramli
Thanks, Nadia. I actually remembered that unsolved problem of mine after reading your recent answer to [this question](http://stackoverflow.com/questions/845390/javascript-to-get-paragraph-of-selected-text-in-web-page/845398#845398). My main problem is in createRange/htmlText that is IE only technology.
buti-oxa
Yes, the documentation on this is at https://developer.mozilla.org/en/nsISelection
Matthew Flaschen
Are you saying that htmlText method is now supported by Mozilla? It is not in the document linked above.
buti-oxa