Creating an IFRAME using JavaScript

前端 未结 2 1316
忘掉有多难
忘掉有多难 2020-11-29 04:34

I have a webpage hosted online and I would like it to be possible that I could insert an IFRAME onto another webpage using some JavaScript.

How would this be the bes

相关标签:
2条回答
  • 2020-11-29 04:55

    It is better to process HTML as a template than to build nodes via JavaScript (HTML is not XML after all.) You can keep your IFRAME's HTML syntax clean by using a template and then appending the template's contents into another DIV.

    <div id="placeholder"></div>
    
    <script id="iframeTemplate" type="text/html">
        <iframe src="...">
            <!-- replace this line with alternate content -->
        </iframe>
    </script>
    
    <script type="text/javascript">
    var element,
        html,
        template;
    
    element = document.getElementById("placeholder");
    template = document.getElementById("iframeTemplate");
    html = template.innerHTML;
    
    element.innerHTML = html;
    </script>
    
    0 讨论(0)
  • 2020-11-29 05:05

    You can use:

    <script type="text/javascript">
        function prepareFrame() {
            var ifrm = document.createElement("iframe");
            ifrm.setAttribute("src", "http://google.com/");
            ifrm.style.width = "640px";
            ifrm.style.height = "480px";
            document.body.appendChild(ifrm);
        }
    </script> 
    

    also check basics of the iFrame element

    0 讨论(0)
提交回复
热议问题