Javascript in iFrame

I used to use the following JavaScript code in my popup windows:


<script type="text/javascript">
<!--
function ChangeColor(color)
   {
   opener.document.getElementById("bgcolor").value = color;
   opener.document.getElementById("xbgcolor").style.background = color;
   window.close();
   }
-->
</script>

After assigning the color to the 2 Id’s, the popup window is closed. It worked perfect!
But now I want to use this code in an iFrame instead of a popup window.
But the code does not seem to work. Any one know how I can do the same thing in iFrame?

Don’t use <!-- –> inside <script> tags.

Are you generating the iframe dynamically and do you want to remove it afterwards?

[B]parent[/B].document.getElementById("bgcolor").value = color;
[B]parent[/B].document.getElementById("xbgcolor").style.background = color;

Yes!! The iFrame is generated dynamically, then when the user chooses his color, the color is assigned to the variable, and the iFrame popup should be closed. Do you know how to dynamically close it?

By the way, why shouldn’t I use <!-- –> inside <script> tags? I’ve seen this for many years to (I believe) hide the content from old browsers that doesn’t support <script> .

Those comments can cause problems if you use XHTML and no usable browser needs them.

Some browsers may block iframe generation.

Try:

var ifr = parent.document.getElementById( "myIframe" /* or whatever its ID is */);

parent.document.getElementById("bgcolor").value = color;
parent.document.getElementById("xbgcolor").style.background = color;

ifr.parentNode.removeChild( ifr );

First there was the Mosaic web browser, and Netscape 1, and JavaScript hadn’t been invented yet. The <!-- –> tags were used by future web browsers to prevent scripts from showing on the page in the Mosaic and Netscape 1 web browsers.
Then there was Netscape 2 and more recent web browsers, and JavaScript was invented. There is no need for <!-- –> tags in all these more recent web browsers.

Netscape 2 came out in 1996, so the <!-- –> tags around script content on the page is only if you have inline scripting on your web page, which you shouldn’t be doing, or if you are providing compatibility for web browsers that were around before 1996, which you shouldn’t be supporting.

tl;dr Don’t use <!-- –> in your scripts