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,调用一个函数读取文本文件的内容,并尽快做任何事情。

更多相关文章

  1. html2canvas 识别 svg 解决方案
  2. HTML5用户身份认证源代码:注册、登录、会话保持的解决方案
  3. 用于在表中强制换行的PHP或HTML/CSS解决方案
  4. 如何实现分布式文件上传解决方案?
  5. 一套PHP做app接口的解决方案
  6. curl获取网页内容出现乱码或为空的解决方案,另附curl_getinfo函数
  7. PHP XAMPP配置PHP环境和Apache80端口被占用解决方案
  8. mysql主从同步报slave_sql_running:no的解决方案
  9. win7下Django的MySql安装,问题解决方案

随机推荐

  1. 分布式作业系统 Elastic-Job-Cloud 源码
  2. 分布式作业系统 Elastic-Job-Lite 源码分
  3. 注册中心 Eureka 源码解析 —— 网络通信
  4. 消息中间件 RocketMQ 源码解析 —— 调试
  5. 分布式作业 Elastic-Job-Lite 源码分析
  6. 基于Spring Boot实现图片上传/加水印一把
  7. 轻量级 Memcached缓存代理 twemproxy实践
  8. 分布式作业系统 Elastic-Job-Lite 源码分
  9. Spring Boot 工程集成全局唯一ID生成器 V
  10. Eureka Server 开启Spring Security Basi