简体   繁体   中英

Unable to get any value from window.getselection

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

textEl.caretPos = document.selection.createRange().duplicate(); 

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 window.getSelection(); window.document.getSelection(); document.getSelection(); window.external.menuArguments.document.getSelection(); window.getSelection(); window.document.getSelection(); document.getSelection(); window.external.menuArguments.document.getSelection(); nothing works. I have already reffered these links

Unable to get property 'createRange' of undefined or null reference

https://tracker.phpbb.com/browse/PHPBB3-12094

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

These dint help either as everywhere they have asked to use window.getSelection().

Here is my code: EDIT:

Please note the below code works fine in IE7/8 and chrome it doesnt work on IE11 as window.getSelection().toString() is empty. Also note this in inside an Iframe if that creates any difference.

/**
         * 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();
            }

        }

Any help will be appreciated thanks a lot in advance.

In case it helps anybody. I was struggling with this and finally figured out it was because I was trying to use window.getSelection() in an invalid context. It does not return selected text in an input:text or textarea. Instead I had to use selectionStart and selectionEnd, and pull the value from the input element and parse the selected text from there. Thanks to http://help.dottoro.com/ljcvonpc.php for this information.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM