0

I want to get some selected text in an iframe and show that text in a search box.

It's first time to use Javascript about iframe, so I really don't know why this code doesn't work.

Can you fix this code please?

jQuery or javascript

frameElement.onmouseup = function() {
    document.getElementById("console").innerHTML = selectText(iframe);
}


function selectText(iframe) {

var frame = document.getElementById(iframe);
var frameWindow = frame && frame.contentWindow; 
var frameDocument = frameWindow && frameWindow.document;
var selectionText = "SelectWord";    

if (frameDocument) { 
    if (frameDocument.getSelection) { 
        selectionText = String(frameDocument.getSelection()); 
    } 
    else if (frameDocument.selection) { 
        selectionText = frameDocument.selection.createRange().text; 
    } 
    else if (frameWindow.getSelection) { 
        selectionText = String(frameWindow.getSelection()); 
    } 
}

return selectionText;
}



</script>

upper line is script part

<body>

<iframe id="iframe" name="iframe" src="https://www.reuters.com/"></iframe>

<div id="console" class="div" type="search"></div>

</body>
Carlos Cavero
  • 3,011
  • 5
  • 21
  • 41
  • see [this](https://stackoverflow.com/questions/5379120/get-the-highlighted-selected-text) and [this](https://developer.mozilla.org/en-US/docs/Web/API/HTMLIFrameElement/contentWindow) and [this](https://stackoverflow.com/questions/1452871/how-can-i-access-iframe-elements-with-javascript) – kemicofa ghost Oct 22 '19 at 15:01

1 Answers1

0

Same Origin Policy...

Use Proxy page...