Blank's blog: http://www.planabc.net/ The use of the innerHTML property is very popular because it provides an easy way to completely replace the contents of an HTML element. Another approach is to use the DOM Level 2 API (removeChild, createElement, appendChild). But it is obvious that using innerHTML to modify the DOM tree is a very easy and effective method. However, you need to be aware that innerHTML has some issues of its own:
There are a few other minor drawbacks that are worth mentioning:
I am more concerned about the security and memory issues associated with using the innerHTML property. Obviously, these are not new problems, and there are already smart people who have figured out ways around some of these issues. Douglas Crockford wrote a cleanup function that is responsible for breaking some circular references caused by event handlers registered with HTML elements and allowing the garbage collector to free the memory associated with these HTML elements. Removing script tags from an HTML string is not as easy as it seems. A regular expression may do what you expect, although it is hard to know whether all possibilities are covered. Here is my solution: /<script[^>]*>[\S\s]*?<\/script[^>]*>/ig Now, let's combine these two techniques into a single setInnerHTML function and bind the setInnerHTML function to YUI's YAHOO.util.Dom: YAHOO.util.Dom.setInnerHTML = function (el, html) { el = YAHOO.util.Dom.get(el); if (!el || typeof html !== 'string') { return null; } // Break the circular reference (function (o) { var a = o.attributes, i, l, n, c; if (a) { l = a.length; for (i = 0; i < l; i = 1) { n = a[i].name; if (typeof o[n] === 'function') { o[n] = null; } } } a = o.childNodes; if (a) { l = a.length; for (i = 0; i < l; i = 1) { c = o.childNodes[i]; // Clear child nodes arguments.callee(c); // Remove all listeners registered on the element through YUI's addListener YAHOO.util.Event.purgeElement(c); } } })(el); // Remove the script from the HTML string and set the innerHTML property el.innerHTML = html.replace(/<script[^>]*>[\S\s]*?<\/script[^>]*>/ig, ""); // Return a reference to the first child node return el.firstChild; }; If there is anything else this function should have or if I'm missing something in the regex, please let me know. Obviously, there are many other ways to inject malicious code on a web page. The setInnerHTML function normalizes the execution behavior of <script> tags on all A-grade browsers only. If you plan to inject untrusted HTML code, make sure to filter it on the server first. There are many libraries that can do this. Original article: The Problem With innerHTML by Julien Lecomte |
<<: How MySQL handles implicit default values
>>: Dockerfile implementation code when starting two processes in a docker container
Let’s not waste any more time and get straight to...
Why do we need to summarize the browser compatibi...
During development, a good user interface will al...
Table of contents 1 Version and planning 1.1 Vers...
background go-fastdfs is a distributed file syste...
The target attribute of a link determines where th...
This article introduces the sample code for imple...
This article example shares the specific code of ...
Preface It took two days to reconstruct a puzzle ...
MVCC MVCC (Multi-Version Concurrency Control) is ...
Table of contents Diffing Algorithm Layer-by-laye...
A WeakMap object is a collection of key/value pai...
Meta declaration annotation steps: 1. Sort out all...
Effect picture (if you want a triangle, please cl...
Copy code The code is as follows: <html> &l...