Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

access json content inside an iframe?

Status
Not open for further replies.

shadedecho

Programmer
Oct 4, 2002
336
US
I've been banging my head on this topic all day.

I want to have an iframe load a url which will return nothing but json. And the url will be same domain, so no cross-domain issues. And I want to then access that json from parent document. I've tried several dozen variations of this, to no success thus far:

Code:
var iframe = document.getElementById("myiframe");
var iwin = iframe.contentWindow || iframe;
var doc = iwin.document;
alert(doc.innerHTML);

There has to be a good cross-browser way to access the contents of an iframe if it's not a standard HTML file, right? How do I do it?
 
From your initial line, use these:

Code:
var theWin = iframe.contentWindow;
var theDoc = iframe.contentDocument || iframe.contentWindow.document;

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
It wasn't.

However, should you want to use it, you could try something like this:

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml"[/URL] dir="ltr" lang="en-US">
<head profile="[URL unfurl="true"]http://gmpg.org/xfn/11">[/URL]
	<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
	<meta http-equiv="content-language" content="en" />
	<title>JSON test</title>

	<script type="text/javascript">
		function parseJson() {
			var theFrame = document.getElementById('jsonFrame');
			var theWin = theFrame.contentWindow;
			var theDoc = theFrame.contentDocument || theFrame.contentWindow.document;

			var theJson = theDoc.body.innerHTML;
			alert(theJson);
		}
	</script>
</head>

<body>
	<iframe id="jsonFrame" src="jsonData.txt"></iframe>

	<p><a href="javascript:parseJson();">Parse JSON</a></p>
</body>
</html>

Having a plain text file in the same folder as the above code alerts the text loaded into the iframe in IE6, at least...



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top