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 3 of 3
  1. #1
    Senior Coder gnomeontherun's Avatar
    Join Date
    Sep 2007
    Location
    Houston
    Posts
    2,846
    Thanks
    10
    Thanked 238 Times in 229 Posts

    iFrame reference

    I'm trying to call a javascript function inside of an iframe, from the parent window. I even am using this function which may or may not be working.

    Code:
    var getiframe= getIFrameDocument("stupidiframe");
    getiframe.alertTest();
    
    function getIFrameDocument(framename) {
    var videoFrame = 
    	  document.getElementById(framename);
    var doc = videoFrame.contentDocument;
    if (doc == undefined) // Internet Explorer
     doc = videoFrame.contentWindow.document;
    
    return doc;
    }
    Of course I have an iframe in the body like this.

    Code:
    <iframe id="stupidiframe" name="stupidiframe" allowtransparency="true" src="example.html" width="400" height="300" marginwidth="0" marginheight="0" hspace="0" vspace="0" frameborder="0" scrolling="no"></iframe>
    And inside the iframe the page has a function like

    Code:
    alertTest {
    alert("Testing...");
    }
    How can I get this to work? I'm pretty sure its how I try to call the function inside of the iframe.

  • #2
    Regular Coder mjlorbet's Avatar
    Join Date
    Jan 2008
    Location
    Milwaukee, WI
    Posts
    724
    Thanks
    8
    Thanked 96 Times in 95 Posts
    if the function is global within the scope of the iframe, you need to reference it in the window scope

    Code:
     
    yourIframe.contentWindow["yourFunction"]();
    -Mike
    "Want me to precludify him, like some kind of dispatcherator?... Can do!" -Bender

  • #3
    Senior Coder gnomeontherun's Avatar
    Join Date
    Sep 2007
    Location
    Houston
    Posts
    2,846
    Thanks
    10
    Thanked 238 Times in 229 Posts
    Ok I'm still not getting this to work, and I don't know if there is any connection to the contents of the page having Flash. I rearranged the code and have a different situation with a similar problem. I found some code from Aero, which opens the iframe just fine. Now I want to close it

    Code:
    <script language="javascript" type="text/javascript">
    
    function showIFrame()
    {
    if (!document.getElementById("videoframe")) {
    var iframe = document.createElement("iframe");
    iframe.setAttribute("id","videoframe");
    iframe.setAttribute("name","videoframe");
    iframe.setAttribute("height","370");
    iframe.setAttribute("width","490");
    iframe.setAttribute("frameBorder","1");
    iframe.setAttribute("scrolling","no");
    iframe.setAttribute("src","example.html");
    iframe.style.position = "absolute";
    iframe.style.display = "block";
    iframe.style.bottom = 0 + 'px';
    iframe.style.left = 0 + 'px';
    window.document.body.appendChild(iframe);
    return false;
    }
    else {
    document.getElementById("videoframe").style.visibility = "visible";
    document.getElementById("videoframe").style.display = "block";
    }
    }
    
    function openWindow(){
    	showIFrame();
    }
    
    // Closes window
    function closeWindow() {
    	document.getElementById("walkonvideoframe").style.visibility = "hidden";
    	document.getElementById("walkonvideoframe").style.display = "none";
    	document.getElementById("walkonvideoframe").width = 0;
    	document.getElementById("walkonvideoframe").height = 0;
      	var iframe = document.getElementById('walkonvideoframe');
       	iframe.parentNode.removeChild(iframe);
    }
    
    </script>
    
    <br /><a href="#" onclick="openWindow();">Play Video</a>
    <br /><a href="#" onclick="closeWindow();">Close Video</a>
    And in FF it closes the Iframe and kills the Flash application, but in IE I can still hear the audio. Is there a way to take the iframe from the DOM?

    Secondly, then I want to have the contents in the Iframe call the parent document to close the Iframe on video completion. I have the on completion part working, but it doesn't seem to call the parent frame at all.

    Code for the page inside the IFrame

    Code:
    			function closeFlashWindow() {
    				parent.document.getElementById("videoframe").style.display="none";
    				parent.document.getElementById("videoframe").style.visilibity="hidden";
    			}


  •  

    Posting Permissions

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