SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Guru DeNasio's Avatar
    Join Date
    May 2001
    Posts
    830
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Javascript in iFrame

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

    Code:
    <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?
    Ballot-Box.net - free polls for webmasters
    FormLog.com - free form processor

  2. #2
    SitePoint Guru
    Join Date
    Sep 2006
    Posts
    731
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Don't use <!-- --> inside <script> tags.

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

    Code:
    parent.document.getElementById("bgcolor").value = color;
    parent.document.getElementById("xbgcolor").style.background = color;
    Tab-indentation is a crime against humanity.

  3. #3
    SitePoint Guru DeNasio's Avatar
    Join Date
    May 2001
    Posts
    830
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Logic Ali View Post
    Are you generating the iframe dynamically and do you want to remove it afterwards?
    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> .
    Ballot-Box.net - free polls for webmasters
    FormLog.com - free form processor

  4. #4
    SitePoint Guru
    Join Date
    Sep 2006
    Posts
    731
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by DeNasio View Post
    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:
    Code:
    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 );
    Tab-indentation is a crime against humanity.

  5. #5
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,684
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by DeNasio View Post
    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> .
    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
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •