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>