简体   繁体   中英

Passing javascript variable to iframe src

Can someone help me if correcting this code. I couldnt figure out where i went wrong. The javascript variable is not replacing the src.

PS This should run as a Google Chrome extensions .

<html> 
    <head> 
        <script type="text/javascript">
            function convert()
            {   
            var url4="test"
            document.getElementById("link").src=url4;
            } 
        </script>
    </head>
    <body onload="convert()">
        <iframe src="wwwx" id="link" width="100%" height="300">
            <p>Your browser does not support iframes.</p>
        </iframe>
    </body>
</html>

Because the JavaScript is being run before the <iframe> is in the DOM, it will never be able to access it.

Try moving your JS to just after the iframe but before the </body> tag:

<html> 
  <head></head>
  <body>
    <iframe src="wwwx" id="link" width="100%" height="300">
      <p>Your browser does not support iframes.</p>
    </iframe>
   <script>
     function convert() {   
       var url4 = "test";
       document.getElementById("link").src=url4;
     }

     convert(); 
   </script>
  </body>
</html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM