4

I have a trouble with Javascript and PHP: I created a page like this:

<html>
<body>
<iframe id="a" src="iframe.php" width="500" height="200"></iframe>
</body>
</html>

and inside iframe.php, I just echo the JSON string:

<?php
$json = array(
    'status' => 'error',
    'html' => '<span class="cool">This is a string</span>'
);

echo json_encode( $json );

What I want is getting the correct raw string echoed by iframe.php using Javascript (maybe jQuery), and parse it as an JSON object.

But the problem is that I always get the wrong content. I tried both the method described here and jQuery('iframe').contents().find('body').html(), but no luck. The span tag is missed or not formatted correctly.

starball
  • 20,030
  • 7
  • 43
  • 238
Anh Tran
  • 601
  • 8
  • 17
  • Does this answer your question? [Can an iFrame display source code only?](https://stackoverflow.com/questions/7921379/can-an-iframe-display-source-code-only) – gdevdeiv Aug 26 '20 at 15:41
  • I know this is an old question. I think it needs more detail. What exactly was the expected outcome, and what was the actual outcome? I think it should either be edited to provide that information, or closed. – starball Nov 27 '22 at 20:41

1 Answers1

0
<script>
    var frameId = 'a'; // the frame id in your sample code
    var frameHtml = frames[frameId].document.documentElement.innerHTML;
</script>
Vivian River
  • 31,198
  • 62
  • 198
  • 313
  • I've tried the `innerHTML` property, but it doesn't work correctly. Its value is `{"status":"error","html":"This is a string<\/span>"}` which is not correct. – Anh Tran Aug 04 '11 at 05:39
  • I hate to be a stickler, but did you try this code *exactly* as I gave it? The DOM seems to be really tricky with frames. Also, please double and triple-check your code to make sure that this code executes *after* the content is loaded into an iframe. For instance, can you bind a function with this code to a button on your page and then click the button after the frame content loads? Remember that the frame has its own document object so $(document).ready() does not necessarily have access to what's in the frame. – Vivian River Aug 04 '11 at 15:37
  • I tried your code _exactly_ as you gave, and the result I posted in the comment above is what I got. – Anh Tran Aug 04 '11 at 16:23
  • Ah, I see. I carefully reviewed the result you gave in your first comment. It looks like your string is partially html-encoded. I'm a bit confused as to why some of the characters < > & " ' are encoded in the result, but not all of them. I believe that your php code (I don't know PHP yet) might be formatting the output incorrectly. In any event, be sure and Google html encoding if you don't know what it is. – Vivian River Aug 04 '11 at 18:10
  • Actually, the PHP code correct, and the JSON text is correct too. If I view the iframe source, I see this: `{"status":"error","html":"This is a string<\/span>"}` which is a valid JSON text. My concern is why the Javascript (maybe browser) doesn't get that text properly. – Anh Tran Aug 05 '11 at 04:18