views:

118

answers:

2

I am extracting content selected by the user via this function:

 function getSelectionHTML() {

  var userSelection;
  if (window.getSelection)
  {

   // W3C Ranges
   userSelection = document.getElementById('axPage').contentWindow.getSelection();
   // Get the range:
   if (userSelection.getRangeAt)
    var range = userSelection.getRangeAt (0);
   else
   {
    var range = document.createRange ();
     range.setStart (userSelection.anchorNode, userSelection.anchorOffset);
     range.setEnd (userSelection.focusNode, userSelection.focusOffset);
   }

   var clonedSelection = range.cloneContents ();
   var div = document.createElement ('div');
   div.appendChild (clonedSelection);
   return div.innerHTML;

  }
  else if (document.selection)
  {
userSelection = self.frames['axPage'].document.selection.createRange();
   return userSelection.htmlText;
  }
  else
  {

   return '';

  }

  };

Extraction is working fine, but I do not seem to find a way to get the original markup of the selected text in Internet Explorer. Is there any way to get a DocumentFragment like in Firefox? I know about the different handling and approach of both systems, but maybe someone has found a working approach in javascript / ECMA to get rid of the nasty ...jQueryXXXXXXXXXXXXX="12" addons in HREFs and that painful uppercase tag- and attributenames.

Again: it is essential to get the exact markup selected.

Any help is appreciated, thank you.

+1  A: 

Your only options in IE are writing code to walk the DOM between the selection boundary points or using htmlText from the selection TextRange as you are doing. The first option will be pretty involved. I don't really understand what your problem is with the second approach: could you be more precise or give an example of what's wrong with the HTML you get from htmlText?

Tim Down
A: 

Thank you for your help, I finally figured it out. If you need to get selected text in Internet Explorer with tags to lower-case try this http://codingforums.com/showthread.php?t=148450 function and if you use jquery and have nasty little .jQueryXXXXXXXXXXXXX="12" 13, 14 right after every single href=""-closing " your solution is to copy your markup to a div and read its innerHTML.

Hope that helps

Peavey