![](/img/trans.png)
[英]Get Html Code, replace parts and store in variable as string without changing the DOM Element
[英]get dom from html string code without executing it
我需要解析一個html字符串以獲取特殊的節點,並丟棄其他腳本標簽
例如,我使用此代碼;
//I get the htmlCode from a textArea
htmlCode = '<video>'+
'<source src="/media/video.oga">'+
'<source src="/media/video.m4v">'+
'<script src="evilscript.js"></script>'+
'</video>';
var div = document.createElement('div');
div.innerHTML = htmlCode;
從那里,我可以訪問div的節點並丟棄不必要的節點; 但我在“網絡”標簽中意識到,任務會啟動視頻源的請求。 而且我也不想發出任何請求,因為htmlCode中可以包含任何惡意腳本。 那么,如何在不啟動httprequests的情況下修改htmlCode?
我想到了documentFragment,但是它不能使用innerHTML,而是使用appendChild。
因此我想到了document.implementation.createHTMLDocument()
。
因此,我對其進行了測試,並成功了。 它不會從源發出任何http請求。
現在是我的代碼:
var dom = document.implementation.createHTMLDocument();
dom.body.innerHTML = '<video>'+
'<source src="/media/video.oga">'+
'<source src="/media/video.m4v">'+
'<script src="evilscript.js"></script>'+
'</video>';
從這里我可以訪問dom。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.