6

Partial Code:

My below code pulls a query from my DB and then uses inner.HTML = to display the data inside a div. It works fine in it original use....

However the below version is called inside a iFrame as it is used to update the page.

The page has no errors and the JavaScript fires however the last line does not work...

I have just realized that perhaps since it is loading in the hidden iFrame it is trying to set the innerHTML of a div inside the iFrame and that of course will not work.

Is this what is happening? It doesn't make sense because I have another script that calls JavaScript at the end of it in the same manner and it works fine.

<?php
    while ($row = mysql_fetch_array($result))
    {
        $p = $p.'<div id="left"><img src="http://www.sharingizcaring.com/bleepV2/images/thumbs/'.$row[image].'" /></div>';
        $p = $p.'<div id="right"><h2>'.$row[artist].' - '.$row['title'].'</h2><br>'.$row['message'].'<br>';
        $p = $p.'<a href="http://www.sharingizcaring.com/bleepV2/'.$row[username].'">'.$row[username].'</a> '.$row[date].'</div>';
        $p = $p.'<div style="clear: both;"></div>';
        $p = $p.'<div id="dotted-line"></div>';
    }


    $p = addslashes($p);
?>

<script>

        alert('posts are firing? ');

        document.getElementById('posts').innerHTML = 'why doth this faileth?';
</script>
Kip
  • 107,154
  • 87
  • 232
  • 265
ian
  • 11,605
  • 25
  • 69
  • 96

4 Answers4

9

You can do it! Read here for more info

You can affect the document with contains the iframe by setting and getting variables from the window element:

// normally you use...
var whatever = "value";

// use the window object instead, which is shared 
// between the iframe and the parent
window.whatever = "value";

The other thing you should know is that you can access the main document via the parent object

inside the iframe you can use...

parent.someattr;

// or try this
parent.getElementById('some_element');
Jiaaro
  • 74,485
  • 42
  • 169
  • 190
  • I just realized the parent. was part of my problem. How do I window? window.getElementById and such and such? – ian Jun 04 '09 at 17:36
  • the "window." part is just an even more global scope that you can use for variables. – Kip Jun 04 '09 at 17:39
  • note: I don't recommend using window unless you HAVE TO... scope is a beautiful and useful thing... and it's useless if everything is global – Jiaaro Jun 04 '09 at 17:50
2

I think what you want is:

parent.getElementById('posts').innerHTML = 'why doth this faileth?';
Kip
  • 107,154
  • 87
  • 232
  • 265
1

The part in the iframe isn't considered the same document.

Jimbo
  • 320
  • 3
  • 14
1

you have to use parent.

user105033
  • 18,800
  • 19
  • 58
  • 69