Dynamically create and read from an IFRAME

Hi,

I am trying to dynamically cfreate an IFRAME, load a page into it, and read the text from that page/frame.

the create iframe part works well, but the "reading from iframe" fails....

why is that, and how can I do it,

I am targeting mainly IE6 , IE7 and IE8 , FF would be nice to have...
ifrm = document.createElement("IFRAME");
		ifrm.setAttribute("id", "myFrame2");
		ifrm.setAttribute("src", "http://server.com/test.asp");
		ifrm.style.width = 640+"px";
		ifrm.style.height = 480+"px";
		document.body.appendChild(ifrm); 
 
 
document.write ("#### " + window.frames['myFrame2'].document.body.innerHTML);

Open in new window

LVL 1
moshemAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Pravin AsarPrincipal Systems EngineerCommented:
You can not read the iframe contents for the security reasons (if the iframe contents are coming from different site)




1. If the iframe src is on a different domain or server than the page that
contains it, you cannot read any of the src document's properties with
javascript.

2. document.getElementById('target_upload').innerHTML contains what is in the iframe tag only-

3. If you are on the same origin you get to the document in the iframe with
var D=document.getElementById('target_upload').contentWindow.document
Then use

 D.documentElement.innerHTML

or
D.body.innerHTML

to return the source code from the document in the iframe
0
moshemAuthor Commented:
I tried what you said, using both IE and FF

IE stopped code execution in the

var D=document.getElementById('myFrame2').contentWindow.document;

while FF continued till the end, but both never displayed the content of the frame..

right now.. the html and the iframe are both on the same server.
		ifrm = document.createElement("IFRAME");
		ifrm.setAttribute("id", "myFrame2");
		ifrm.setAttribute("src", "http://www.myserver.com/test.asp");
		ifrm.style.width = 640+"px";
		ifrm.style.height = 480+"px";
		document.body.appendChild(ifrm); 
		
		document.write ("##");
		
		var D=document.getElementById('myFrame2').contentWindow.document;
		
		document.write ( D.documentElement.innerHTML  + "$$" );

Open in new window

0
fsze88Commented:
like this?
http://www.kennam.com/test/ee24432429.html
<html>
<head>
<script type="text/javascript">
var MSIE = navigator.userAgent.indexOf('MSIE')>=0?true:false;
 
 
 
function getDivContent(){
 
  var ifrDoc;
  if (MSIE){
    ifrDoc=document.getElementById('myFrame2').contentWindow.document;
  }else{
    ifrDoc=document.getElementById('myFrame2').contentDocument;
  }
  alert(ifrDoc.body.innerHTML);
}
 
</script>
</head>
 
<body >
 
 
<!--<iframe src="http://www.kennam.com/test/test.php" id="myFrame2" ></iframe>-->
 
<input type="button" onclick="getDivContent()" value="Get text" />
 
</body>
 
<script type="text/javascript">
ifrm = document.createElement("iframe");
ifrm.setAttribute("id", "myFrame2");
ifrm.setAttribute("src", "http://www.kennam.com/test/test.php");
ifrm.style.width = 640+"px";
ifrm.style.height = 480+"px";
document.body.appendChild(ifrm);
 
</script>
</html>

Open in new window

0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

moshemAuthor Commented:
ok,

that works.

but what if I needed the IFrame content to come from another web server ?

how could that be achieved ?
0
fsze88Commented:
no idea, it is internet security problem.
you may needs to run on server side scripting (asp,aspx,php,jsp .......) to get the content of internet site
0
moshemAuthor Commented:
thats exactly what I am doing..

I am trying to get the full list of languages supported in IE, the only time IE reports those is when it creates an initial HTTP request...

so I created an IFRAME which reads an ASP file off a server which in return sends back the language header from IE.

now all I have to do is to read it... and I can't

any ideas?
0
fsze88Commented:
You means you needs to read multi language from you computer and/or server?
if yes, please search from www.microsoft.com or www.msdn.com about Multilingual about your computer/server. (xp,vista,server 2000,2003,2008....?)
After installed it, should works..
0
moshemAuthor Commented:
no, I mean at runtime...

I wrote a gadget that changes it's interface language according to the detected language.

It will reside on many servers, and it needs to get the full list of supported languages as defined in IE "languages preferences" button locate at the internet options dialog.

most javascript methods I know, only provide you with the first language, but no the full list.
0
fsze88Commented:
moshem,
I think there is no way to detect all of supported language on client ie by javascript. (I read through https://developer.mozilla.org/En, http://www.w3schools.com/ already)
The only way may be using asp component like http://www.example-code.com/asp/asp-language-detection.asp
You needs to take times to search from google or yahoo about asp components....
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
moshemAuthor Commented:
this component checks for a string,.


the way to do it is to get the data from an ASP page... just by returning the ACCEPT-LANGUAGE header.

the problem is as I said, I can't read it via flash, since IE sends flash only the first one.

that is why I went to the IFRAME solution, but I am stuck there too...

any idea you can think of ?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Languages and Standards

From novice to tech pro — start learning today.