简体   繁体   English

如何在javascript中显示一个html文件?

[英]How to display an html file in javascript?

I am trying to display an html file in my js-code.我试图在我的 js 代码中显示一个 html 文件。

It is an ordinary html file that I would like to see.这是一个普通的 html 文件,我想看。 I have already created an IFRAME for this, but somehow I cannot go further...我已经为此创建了一个 IFRAME,但不知何故我不能更进一步......

This is my code fragment:这是我的代码片段:

var controlAddIn = document.getElementById('controlAddIn').src = 'index.html';

So the file I want to call is "index.html".所以我要调用的文件是“index.html”。 But it is not working.但它不起作用。

If I call it in that way:如果我这样称呼它:

var controlAddIn = document.getElementById('controlAddIn').srcdoc = '<div><meta charset="UFT-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"></div>';

It is working, but taht makes it very hard to read... Do you have an idea, how I can call my entire code at this point, also multiline?它正在工作,但它很难阅读......你有什么想法,我现在如何调用我的整个代码,也是多行的?

The both file should be same path if you are calling like that else you have call the correct path如果您像这样调用,则这两个文件应该是相同的路径,否则您调用了正确的路径

var controlAddIn = document.getElementById('controlAddIn').src = 'index.html';

save this file in index.html path and open it on browser将此文件保存在 index.html 路径中并在浏览器中打开它

<script type="text/javascript">

function newSite() {


    document.getElementById('myIframe').src = "index.html"
}    
</script>

<input type="button" value="Change site" onClick="newSite()" />
<iframe id="myIframe" src="http://google.com/" ></iframe>

You Could use XMLHttp request to get Html File Content this code async您可以使用 XMLHttp 请求异步获取此代码的 Html 文件内容

 function httpGet(theUrl,callback) { var xmlHttp = new XMLHttpRequest(); xmlHttp.open( "GET", theUrl, true ); // false for synchronous request xmlHttp.send( null ); xmlHttp.onreadystatechange = function() { callback(xmlHttp.responseText); }; } var controlAddIn; httpGet("index.html",function(content){ controlAddIn = document.getElementById('controlAddIn').src =content; });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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