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 4 of 4
  1. #1
    New Coder
    Join Date
    Dec 2003
    Posts
    31
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Read/Write to/from iframe

    Hi everyone,

    I am having problems with accessing the innet HTML code of iframe tag. What I'm trying to do is:

    var htmlcode = iframe.document.body.innerHTML;

    The debugger says that the object doesn't support this property or function.

    Thanks a lot everyone

  • #2
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    First of all, you must make sure that the variable iframe you use is actually a DOM reference to the iframe. Second, you access the wrong property, you should use HTMLIFrameElement's contentDocument in DOM compliant browsers.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #3
    New Coder
    Join Date
    Dec 2003
    Posts
    31
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I am kind of not the best JS programmer. I use IE 5.5+. What's DOM?

    Thanks

  • #4
    Senior Coder
    Join Date
    Jun 2002
    Posts
    1,404
    Thanks
    2
    Thanked 32 Times in 32 Posts
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" 
        "http://www.w3.org/TR/html4/loose.dtd">
    
    <html>
    <head>
    <title>untitled</title>
    <style type="text/css">
    
    iframe {
            display: block;
    	margin: 40px auto;
            width: 440px;
            height: 260px;
            border-width: 0;
           }
    
    div    {
            text-align: center;
           }
    
    </style>
    <script type="text/javascript" language="javascript">
    
    function get_IFRAME_doc(iframe_id)
    {
    	var oIframe = document.getElementById(iframe_id);
    	if (oIframe != null)
    	{
    		if (oIframe.contentDocument) 
    			return oIframe.contentDocument; 
    		else if (oIframe.contentWindow) 
    			return oIframe.contentWindow.document;
    		else if (oIframe.document) 
    			return oIframe.document;
    		else return null;
    	}
    }
    
    function write_to_iframe(iframe_id)
    {
    	sHTML = '<body style="font:14px verdana;color:white;background:purple;">';
    	sHTML += '<h4>Iframe Content</h4>';
    	sHTML += 'bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla ';
    	sHTML += 'bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla ';
    	sHTML += 'bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla ';
    	sHTML += 'bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla ';
    	sHTML += 'bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla ';
    	sHTML += 'bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla ';
    	sHTML += 'bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla ';
    	sHTML += 'bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla ';
    	sHTML += '<h5>The end.</h5></body>';
    	var oIframe = document.getElementById(iframe_id);
    	oIframe.src = 'javascript&#58;parent.sHTML';
    }
    
    function read_from_iframe(iframe_id)
    {
    	var oIframe = document.getElementById(iframe_id);
    	if (IFrameDoc = get_IFRAME_doc(iframe_id))
    		alert(IFrameDoc.getElementsByTagName('BODY').item(0).innerHTML);
    }
    
    </script>
    </head>
    <body>
    <iframe name="mishmash" id="mishmash" frameborder="0"></iframe>
    <div>
    <button onclick="write_to_iframe('mishmash')">write to <strong>iframe</strong></button>&nbsp;
    <button onclick="read_from_iframe('mishmash')">read from <strong>iframe</strong></button>
    </div>
    </body>
    </html>
    http://safari.oreilly.com/JVXSL.asp?...c=1&u=1&r=&o=1
    http://www.sitepoint.com/article/872


  •  

    Posting Permissions

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