使用javascript在外部URL中搜索文件

Searching an external URL for files with javascript

本文关键字:搜索 文件 URL 外部 javascript 使用      更新时间:2024-02-04

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

 <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。(Javascript的新PS)

不确定这是否是最干净的方法,但您可以在页面上添加一个隐藏的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
});