Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Apr 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Show div within an iframe

    On my page there's an iframe containing a div which I've hidden with code on the actual iframe page. I want the iframe content only to be accessible by clicking a button on the main page.
    Here's the main parts of the code:

    Within the iframe:

    Code:
    <html>
    <head>
    <script language="javascript">
    <!--
    
    var state = 'none';
    
    function showhide(layer_ref) {
    
    if (state == 'block') {
    state = 'none';
    }
    else {
    state = 'block';
    }
    if (document.all) { 
    eval( "document.all." + layer_ref + ".style.display = state");
    }
    if (document.layers) { 
    document.layers[layer_ref].display = state;
    }
    if (document.getElementById &&!document.all) {
    hza = document.getElementById(layer_ref);
    hza.style.display = state;
    }
    }
    //-->
    </script> 
    </head>
    <body onload="showhide('div')" style="display: none;">
    <div id="div" name="div">
    Some Content...
    </div>
    </body>
    </html>
    And on the main page:

    Code:
    <html>
    <head>
    </head>
    <body>
    <div id="div1">
    <iframe onload="iFrameHeight()"	id="blockrandom"
    	name="iframe"
    	src="http://somewhere.com/"
    	width="100%"
    	height="500"
    	scrolling="auto"
    	align="top"
    	frameborder="0"
    	class="wrapper">
    	This option will not work correctly. Unfortunately, your browser does not support inline frames.</iframe>
    </div>
    <div id="div2">
    <input type="button" value="Show/Hide Search" onclick="document.getElementById('blockrandom').contentWindow.showhide('div')">
    </div>
    </body>
    </html>
    The page hides successfully but I can't get it to show again from the button on the main page.
    If there's something wrong with my code or if there was a way that the iframe page could check if it was loaded on the right page or not would be great although i'm not sure if that's possible.

    Any solutions or alternatives are welcome. Thanks.

    P.S. This is on Joomla CMS, I don't think it should make a difference but correct me if I'm wrong.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,084
    Thanks
    80
    Thanked 4,553 Times in 4,517 Posts
    If the IFRAME is not from the *same domain* as the main page, then you can *NOT* use JavaScript to access anything internal to the iframe, at all.

    The example code you show there does
    Code:
    src="http://somewhere.com/"
    which makes me suspicious that the iframe code is, indeed, from a different domain.

    Also, the JS code you show there is *ANCIENT*. It was designed for use with MSIE 4 and Netscape 4, both of which are hopefully dead lo these many years. And it even has support for MSIE 3, in the form of the <!-- and --> crap.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Tags for this Thread

    Posting Permissions

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