使用documentFragment来解析HTML而不发送HTTP请求 [英] Using documentFragment to parse HTML without sending HTTP requests

查看:172
本文介绍了使用documentFragment来解析HTML而不发送HTTP请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想解析一个字符串,使DOM树离开它。我决定使用 documentFragment API,我到目前为止这样做:

I'd like to parse a string and make DOM tree out of it. I decided to use documentFragment API and I did this so far:

var htmlString ="Some really really complicated html string that only can be parsed by a real browser!";
var fragment = document.createDocumentFragment('div');
var tempDiv = document.createElement('div');
fragment.appendChild(tempDiv);
tempDiv.innerHTML = htmlString;
console.log(tempDiv);

但问题是这个脚本导致浏览器(Chrome专门)发送实际的HTTP请求!我是什么意思?以此为例:

But the problem is that this script causes my browser (Chrome specifically) to send actual HTTP requests! what do I mean? take this as example:

var htmlString ='<img src="somewhere/odd/on/the/internet" alt="alt?" />';
var fragment = document.createDocumentFragment('div');
var tempDiv = document.createElement('div');
fragment.appendChild(tempDiv);
tempDiv.innerHTML = htmlString;
console.log(tempDiv);

导致:

是否有任何解决方法?或任何其他更好的想法来解析HTML-String?

Is there any workarounds for this? or any other better idea to parse HTML-String?

推荐答案

我在stackoverflow上找到了我的问题的答案,这个答案。答案包括一段代码,它使用本机浏览器功能解析HTML,但是在不发送HTTP请求的半沙盒环境中。希望它也能帮助别人。

I've found answer of my question here on stackoverflow, this answer. the answer consists of a piece of code which parses HTML using native browser functionality but in a semi-sandboxed environment which doesn't send HTTP requests. hope it helps others as well.

这篇关于使用documentFragment来解析HTML而不发送HTTP请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆