[英]How to load external HTML file into Javascript document object
I am looking for the javascript method analogous to PHP's DOMDocument->loadHTMLFILE(), so that I can parse an external html file's contents and extract images. 我正在寻找类似于PHP的DOMDocument-> loadHTMLFILE()的javascript方法,以便可以解析外部html文件的内容并提取图像。 Right now i'm doing it via ajax, which is too slow.
现在我正在通过ajax来做,这太慢了。
Here is the PHP i use to scrape images, it works. 这是我用来抓取图像的PHP,它可以工作。 I simply want to do the same thing but browser side so that its faster.
我只是想在浏览器方面做同样的事情,以便更快。
if(isset($_POST['link']) && $_POST['link'] !== ""){
//extract relevant article info from link
$sourceArray = array();
$sizeArray = array();
$link = $_POST['link'];
//generate new DOMdoc
$article = new DOMDocument;
$article ->loadHTMLFile($link);
//get the largest image
$images = $article->getElementsByTagName("img");
foreach($images as $image){
$source = $image->getAttribute("src");
if(strpos($source, "http://") !== false){
$sizeProfile = getimagesize($source);
$imgArea = $sizeProfile[0] * $sizeProfile[1];
if($imgArea > 100){
array_push($sizeArray, $imgArea);
array_push($sourceArray, $source);
}
}
}
array_multisort($sizeArray, SORT_DESC, $sourceArray);
$sourceHTML = "";
$i = 0;
foreach($sourceArray as $source){
$id = 'image'.$i;
$sourceHTML .= '<img id="'.$id.'" class="notSelectedPicture" src="'.$source.'" onclick="toggleSelectedPicture(\''.$id.'\');" alt="alt">';
$i++;
}
echo $sourceHTML;
exit();
}
The ajax solution works for this purpose. 为此,ajax解决方案适用。 As a client-side language JS does not seem to be capable of getting and parsing external html files in the way that PHP is.
作为一种客户端语言,JS似乎不能够以PHP的方式获取和解析外部html文件。 In order to cut down on loading time, one should focus on the efficiency of the dom parsing code that the ajax posts to.
为了减少加载时间,应该关注ajax发布到的dom解析代码的效率。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.