简体   繁体   English

如何从文本文件中随机更改 iframe 的 src

[英]how to randomly change the src of an iframe from a text file

I want to change the src of an iframe randomly from a text file,so that when a user opens that iframe a different url is opened in iframe.我想从文本文件中随机更改 iframe 的 src,以便当用户打开该 iframe 时,会在 iframe 中打开不同的 url。

<html>
<head>
  <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
</head>
<body>

    <script type="text/javascript">
        function read() {
            var txtFile = new XMLHttpRequest();
            txtFile.open("GET", "https://9to5notes.com/new/add.txt",true);
            txtFile.onreadystatechange = function () {
                if (txtFile.readyState === 4) {  
                    // Makes sure the document is ready to parse.
                    if (txtFile.status === 200)
                    {  
                        var alltext=txtFile.responseText;
                        var linetext=txtFile.responseText.split("\n");
                        var delimeter = '^';
                        var text0= linetext[0];
                        var splitted = text0.split(delimeter);
                        console.log(splitted[1]);
                        $('#iframe1').attr('src', splitted[1]);
                    }
                }
            }

            txtFile.send(null)
        }
        $(function(){
            read();
        });
    </script>

    <iframe id="iframe1" src="" />
</body></html>

the text file looks like this Site1^ https://www.google.com/ Site2^ https://www.facebook.com/文本文件看起来像这样 Site1^ https://www.google.com/ Site2^ https://www.facebook.com/

First read the text file with PHP首先用PHP读取文本文件

And store URL's into array of JavaScript并将 URL 存储到 JavaScript 数组中

Then with random function place into iframe然后用随机函数放入 iframe

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

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