开发者

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.

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜