当html从本地文件系统运行时,如何获取的内容?
16lz
2021-01-22
There is a question asking how to get content of <script src="...">
. The answer is to use ajax for that. However, this will not work when the html file is not hosted on web server. Is there another option that work for html files accessed both from web server and from local file system?
有一个问题询问如何获取
1 个解决方案
#1
0
To read the file:
要阅读文件:
<base href="file:///C:/path/to/your/folder/"/>
<script>
window.onload = function(){
var iframe = document.createElement('iframe');
iframe.id = 'iframe';
iframe.style.display = 'none';
document.body.appendChild(iframe);
iframe.src = 'myJs.js';
setTimeout(function(){
var text = document.getElementById('iframe').contentDocument.body.firstChild.innerHTML;
alert(text);
}, 1000);
}
</script>
or you can
或者你可以
<script>
function readfile() {
alert(document.getElementById('iframe').contentDocument.body.firstChild.innerHTML);
}
</script>
<iframe id='iframe' src = 'test.txt' onload='readfile()'> </iframe>
attach an onload event to the iframe calling a function reading the contents of the text-file an doing whatever as soon as possible.
将一个onload事件附加到iframe,调用一个函数读取文本文件的内容,并尽快做任何事情。
更多相关文章
- html2canvas 识别 svg 解决方案
- HTML5用户身份认证源代码:注册、登录、会话保持的解决方案
- 用于在表中强制换行的PHP或HTML/CSS解决方案
- 如何实现分布式文件上传解决方案?
- 一套PHP做app接口的解决方案
- curl获取网页内容出现乱码或为空的解决方案,另附curl_getinfo函数
- PHP XAMPP配置PHP环境和Apache80端口被占用解决方案
- mysql主从同步报slave_sql_running:no的解决方案
- win7下Django的MySql安装,问题解决方案