Javascript rotator如何从文本文件中读取VAR的URL列表

时间:2016-08-24 13:15:13

标签: javascript iframe

以下代码基于Rotating URLs within an iframe

    <iframe src="http://example.com/file01.html" id="rotator"></iframe>
<script type="text/javascript">
    // start when the page is loaded
    window.onload = function() {

        var urls = [
            "http://example.com/file02.html",
            "http://example.com/file03.html",
            "http://example.com/file04.html"
        ];

        var index = 1;
        var el = document.getElementById("rotator");

        setTimeout(function rotate() {

            if (index === urls.length) {
                index = 0;
            }

            el.src = urls[index];
            index = index + 1;

            // continue rotating iframes
            setTimeout(rotate, 5000);

        }, 5000); // 5000ms = 5s
    };
</script>

但是如何修改代码以从单独的文本文件中读取此URL列表? 不

var urls = [
    "http://example.com/file02.html",
    "http://example.com/file03.html",
    "http://example.com/file04.html"
];

但是类似

var urls = /list.txt
建议

尝试将上面的块更改为

var urls = new XMLHttpRequest();
urls.open('GET', '/list.txt');
urls.onreadystatechange = function() {
  alert(urls.responseText);
}
urls.send();

也尝试了

var urls = new XMLHttpRequest();
urls.onreadystatechange = function() {
if (urls.readyState == 4) {
 alert(urls.responseText);
}
};
urls.open("GET", "list.txt", true);
urls.send(null);

但两者都没有成功,iFrame崩溃了。 感谢任何其他想法或提示尝试

0 个答案:

没有答案