Click here to Skip to main content
15,908,264 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have a Iframe and a textbox within my webpage. Iframe contain a link of webpage (like: http://www.codeproject.com/). I would like to copy some text from the iframe content to textbox on it's "mouseover" even.

I have tried using the following code -

C#
function getIframeSelectionTextSource(tex) {

           var win = document.getElementById("frame").contentWindow ;

           var doc = win.document;
           if (win.getSelection) {
               if (win.getSelection().toString()!='') {
                   $("#" + tex.id).val(win.getSelection().toString());
                   return win.getSelection().toString().trim();
               }
           }
           else if (doc.selection && doc.selection.createRange) {
               $("#" + tex.id).val(doc.selection.createRange().text);
               return doc.selection.createRange().text.trim();
           }
       }



but when I am trying to var win = document.getElementById("frame").contentWindow ; it said "Access denied".

Please help. Thanks in advance.
Posted
Comments
Does that iframe belong to different domain?
Member 11207632 13-Aug-15 4:13am    
Yes. It content website links like www.codeproject.com or www.google.com
You can't.
Sanket Saxena 12-Aug-15 7:21am    
Seems your iframe belongs to other domain which is by default prohibited because browsers use same origin policy.
Member 11207632 13-Aug-15 4:13am    
Yes. It content website links like www.codeproject.com or www.google.com

1 solution

If your iframe not belongs to other domain then you can get the elments of that iframe something like:
JavaScript
$('#'+ "your iframe id").contents().find('#'+"your element id");


Hope it helps :)
 
Share this answer
 
Comments
Member 11207632 13-Aug-15 4:14am    
$('#'+ "your iframe id").contents() it returns blank.
Sanket Saxena 13-Aug-15 4:33am    
Yes it should be as you have commented that its from different domain.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900