【问题标题】:Searching an external URL for files with javascript使用 javascript 在外部 URL 中搜索文件
【发布时间】:2015-02-19 16:14:57
【问题描述】:

我正在构建一个页面,该页面需要能够获取网页上的所有文件链接并将它们添加到下拉列表中。原来是脚本应该与文件在同一页面上,但现在它需要搜索外部。这是我改之前用的

 <script>
         $(document).ready(function() {
             var arr = [];
             var filenames = [];
             var alt_var;
             var baseURL = "www.fakeurl.com"
             $('.ms-vb-icon').find('a').each(function(){
         	var temp = $(this).attr('href')
         	$(this).find('img').each(function(){
         	alt_var = $(this).attr('alt');
         	});
         	if(temp.indexOf('.csv') != -1){arr.push(temp); filenames.push(alt_var);}
         });
         	for(i = 0; i < arr.length; ++i)
         	{
         	var x = document.createElement('li');
         	var a = document.createElement('a');
         	var t = document.createTextNode(" " + filenames[i]);
         	var fullURL = baseURL + arr[i];
         	a.setAttribute('href',"#");
         	a.setAttribute('class', "glyphicon glyphicon-file");
         	a.setAttribute('id', baseURL + arr[i]);
           	a.setAttribute('onclick', "drawChart(this.id)");
         	a.appendChild(t);
         	x.appendChild(a);
            document.getElementById("dropdownfiles").appendChild(x);
         	}                      
         });    
      </script>

如何更改它以搜索外部 URL。 (PS Javascript 新手)

【问题讨论】:

  • “外部”URL 是否在同一个域中?
  • 很可能,但它会在公司共享点站点上,所以我希望它可以连接到所有不同的共享点域

标签: javascript html file url dom


【解决方案1】:

不确定这是否是最干净的方式,但您可以在页面上添加一个隐藏的 iframe,然后在其中搜索。

css:

.externalSearcher iframe {
    display: none;
}

html:

<div class="externalSearcher"></div>

js:

$('.externalSearcher').append('<iframe src="' + externalLink + '"></iframe>');
$('.externalSearcher').find('a').each(function () {
    //do what you want with the link
});

【讨论】: