0

I want to get the selected text from the i-frame, where the source is from another domain.

<html>
<head>
<script>
function getselText()
{
var iframe = document.getElementById("my");
var txt = getIframeSelectionText(iframe);

document.aform.selectedtext.value =  txt;

}

function getIframeSelectionText(iframe) {

var win= iframe.contentWindow || iframe.contentDocument.defaultView;
var doc= iframe.contentDocument || win.document || iframe.contentWindow.document;


  if (win.getSelection) {
    return win.getSelection().toString();
  } 
  else if (doc.selection && doc.selection.createRange) {
    return doc.selection.createRange().text;
  }
  else if (doc.selection) {
    return doc.selection.createRange().text;
  }
  else
  {
  alert('Error in rendering');
  }
}
</script>
</head>
<body>
<iframe id="my" frameborder="1" scrolling="yes" width="800" height="400" src="info.php">
            This is I-Frame Content
    </iframe>
<input type="button" value="Get selection" onmousedown="getselText()"> 
<form name=aform >
<textarea name="selectedtext" rows="5" cols="20"></textarea>
</form>
</body>
</html>

This code works well with the source of the i-frame in the same domain. But, I need to get the selected text from the i-frame where the source is from different domain (to which I don't have access to change it).

Example:

<iframe id="my" frameborder="1" scrolling="yes" width="800" height="400" src="http://www.anotherdomain.com">
            This is I-Frame Content
    </iframe>

Please help me in this.

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by fxm
0

get the selected text from the i-frame where the source is from different domain

By design, no modern browser will do that.

For special cases [your own desktop or intranet] more-or-less ugly work-arounds are possible; in the general case, none of them is likely to be practical.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.