title: Unable to get any value from window.getselection
body: <p>In our project we have been using a functionality that when we select some text and right click on it the text is copied and another right click will cause the copied text to be pasted in javascript. The code was working fine until IE11. In IE11 we get error at</p>
  
  <pre><code>textEl.caretPos = document.selection.createRange().duplicate(); 
  </code></pre>
  
  <p>I have researched a lot and found that document.selection is no more supported on IE11 and we need to use window.getSelection() but that didnt work either. I have tried all combinations <code>window.getSelection(); window.document.getSelection(); document.getSelection(); window.external.menuArguments.document.getSelection();</code> nothing works. I have already reffered these links </p>
  
  <p><a href="https://stackoverflow.com/questions/19748500/unable-to-get-property-createrange-of-undefined-or-null-reference">Unable to get property &#39;createRange&#39; of undefined or null reference</a></p>
  
  <p><a href="https://tracker.phpbb.com/browse/PHPBB3-12094" rel="nofollow noreferrer">https://tracker.phpbb.com/browse/PHPBB3-12094</a></p>
  
  <p><a href="https://social.msdn.microsoft.com/Forums/ie/en-US/138e9cbc-aee7-46fc-bb7e-c5112e88497a/unable-to-get-property-createrange-of-undefined-or-null-reference?forum=ieextensiondevelopment" rel="nofollow noreferrer">https://social.msdn.microsoft.com/Forums/ie/en-US/138e9cbc-aee7-46fc-bb7e-c5112e88497a/unable-to-get-property-createrange-of-undefined-or-null-reference?forum=ieextensiondevelopment</a></p>
  
  <p>These dint help either as everywhere they have asked to use window.getSelection().</p>
  
  <p>Here is my code:
  <strong>EDIT:</strong></p>
  
  <p>Please note the below code works fine in IE7/8 and chrome it doesnt work on IE11 as <code>window.getSelection().toString()</code> is empty. Also note this in inside an Iframe if that creates any difference.</p>
  
  <pre><code>/**
           * Copies or Pastes text into a text box.  If the
           * text is selected, then right clicking on it does a copy.
           * If no text is selected, then right clicking invokes a paste
           * of any clipboard text into the textbox.
           *
           * NOTE: Pasting will replace any value already in the textbox
           */
          function copyPasteHelper()
          {
              // if something is currently selected, copy it
              var selectedText = "";
              if(document.selection != null){// for IE 8 and below only
                  storeCaret (event.srcElement);
                  selectedText = document.selection.createRange().text;
              }else if(typeof window.getSelection() != "undefined") // for IE 9+ and Chrome
              {
                  //storeCaret (event.srcElement);
                  selectedText = window.getSelection().toString();
                  alert(selectedText);//this is empty
              }
  
  
              if (selectedText != "")
              {
                  if(window.clipboardData)
                  {
                      window.clipboardData.setData("Text", selectedText);
  
                      var lefter2 = event.offsetY+0;
                      var topper2 = event.offsetX+15;
  
                      // oCopiedPopup.show(topper2, lefter2, 80, 23, window.event.srcElement);
                  }
                  else
                  {
                      jQuery("#clipboard", window.parent.document).val(selectedText);
                  }
              }
              else // if nothing is selected, paste whatever text is in the clipboard
              {
                  pasteHelper();
              }
  
          }
  </code></pre>
  
  <p>Any help will be appreciated thanks a lot in advance.</p>