开发者

Passing javascript variable to iframe src

开发者 https://www.devze.com 2023-03-29 03:28 出处:网络
Can someone help me if correcting this code. I couldnt figure out where i went wrong. The javascript variable is not replacing the 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.

P.S. 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()">
        <i开发者_运维知识库frame 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>
0

精彩评论

暂无评论...
验证码 换一张
取 消