从txt文件中逐个将多个链接插入到一个iframe中

时间:2013-02-24 13:31:28

标签: javascript

我正在尝试将多个src链接放入1帧,然后逐个加载它们。 Heres是我目前拥有的代码:

<script type="text/javascript">
    $(document).ready(function(){   
    $('#1').on('load', function() {     
            $('#1').attr('src', "http://www.example1.com");             
    }).attr('src', "http://www.example2.lt");

    $('#1').on('load', function() {                 
            $('#1').attr('src', "http://www.example1.com");             
    });

    });
</script>

<iframe id="1"  width="100" height="100" src=""></iframe>

在此代码中,它首先加载example1.com网址,然后加载example2.com网址。

我有一个存储网址的txt文件。我怎么做到这样javascript会从txt文件中读取url,将它们放在javascript中,并且加载iframe的次数与txt文件中的url一样多次,一个接一个?也许还有另一种方法可以做到这里我写的那个?

1 个答案:

答案 0 :(得分:0)

我会将文件格式化为json编码的字符串数组,并使用$.ajax$.getJSON从服务器获取它。

现在你已经设置了阵列,你可以:

$.getJSON( ... , function(success) { 
  $('#1').on('load', function() {
    $(this).attr('src', success.pop());
  }).attr('src', 'http://www.google.com');
});

有点像这样:

http://jsfiddle.net/FQLce/