title: How to find selection in HTML document that contains iframe or just frames
body: <p>Is there a way to find the selected text in an HTML document if the text may be within one of its frames (or iframes)?</p>
  
  <p>If the document has no frames it's simple:</p>
  
  <pre><code>var text;
  if (document.getSelection) {
   // Firefox and friends
   text = document.getSelection();
  } else if (document.selection) {
   // IE 
   text = document.selection.createRange();
  }
  if (text == undefined || text == '') {
   // Iterate over all textarea elements and see if one of them has selection
   var areas = document.getElementsByTagName('textarea');
   for(var i = 0; i = areas.length; i++) {
    if(areas[i].selectionStart != undefined &amp;&amp; 
       areas[i].selectionStart != areas[i].selectionEnd){
     text = areas[i].value.substring(areas[i].selectionStart, a[i].selectionEnd);
     break;
    }   
   }   
  }
  // Now if document has selected text, it's in text
  </code></pre>
  
  <p>So this works cross-browser (although not very pretty).</p>
  
  <p>The problem is when the document contains frames or iframes.
  Frames have their own document, so just using the code above is not enough.
  One could potentially iterate over the frames tree and search for selected text in one of them, however in general frames can have content from different domains, so even if I were to iterate over all frames and over all subframes etc of the root document in search of selected text I would not have had the permission to access their HTML, right? So I wouldn't be able to get their selected text.</p>
  
  <p>Is there a (simple) reliable way of finding the selected text on a web page even if the page contains frames?</p>
  
  <p>Thanks </p>