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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.