简体   繁体   English

如何将外部HTML文件加载到Javascript文档对象中

[英]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.

相关问题 如何在 Angular 8 中加载外部 Javascript 和 HTML 文件 - How to Load an external Javascript and HTML file in Angular 8 如何在popup.html中异步加载外部javascript文件? - How to load an external javascript file asynchronously within popup.html? 如何加载外部HTML文件 - How to load an external HTML file 如何在JavaScript中加载外部文件? - How to load an external file in JavaScript? 将HTML文档中的Java语言更改为外部Java语言文件 - Changing Javascript within an HTML document to an external Javascript file 将带有document.write的外部JavaScript文件加载到AngularJS应用程序中 - Load external JavaScript file with document.write into AngularJS app 加载使用document.write的外部javascript文件 - Load external javascript file where document.write is used 如何在div标记内的document.writes()html块进行异步回发时重新加载外部javascript文件? - How to Reload external javascript file on asynchronous postback which document.writes() html block inside a div tag? 如何将外部Javascript文件链接到Play Framework项目中的html文档? - How do I link external Javascript file to a html document in Play Framework project? 如何在HTML文档形式的外部JavaScript文件中访问变量 - How do I access a variable in HTML document form external JavaScript file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM