Javascript gets the value of the element in the page in the iframe

  • 2020-03-30 01:45:32
  • OfStack

IE method:
Document. Frames [' myFrame] document. The getElementById (" test "). The value;

Firefox method:
Document. GetElementById (" myFrame ") contentWindow. Document. The getElementById (" test "). The value;

IE, firefox methods:


    function getValue(){

         var tmp = '';

         if(document.frames){

                tmp += 'ie Brother said: ';

                tmp += document.frames['myFrame'].document.getElementById('test').value;

         }else{

                tmp = document.getElementById('myFrame').contentWindow.document.getElementById('test').value; 

         }

         alert(tmp);

    }

Sample code:
A. code in HTML page

<html>
            <head>
                <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
                <title>
                    javascript  To obtain iframe The value of the element in the page   test 
                </title>
           </head>
           <body>
                <iframe id="myFrame" src='b.html' style="width:300px;height: 50px;"></iframe>
                <input type="button" id="btn" onclick="getValue()" value="test" >
                <script type="text/javascript">
                        function getValue(){
                            var tmp = '';
                            if(document.frames){
                                    tmp += 'ie Brother said: ';
                                    tmp += document.frames['myFrame'].document.getElementById('test').value;
                            }else{
                                    tmp = document.getElementById('myFrame').contentWindow.document.getElementById('test').value; 
                            }
                            alert(tmp);
                        }
                </script>
            </body>
        </html>

B. code in HTML page

       <html>
            <head>
                <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
                <title>
                     I am a  iframe In the page 
                </title>
            </head>
            <body>
                <input type='text' id="test" value=' Welcome to: justflyhigh.com'>
            </body>
        </html>  


Related articles: