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
Code first, then text Copy code The code is as fol...
This article describes how to use MySQL to export...
1. Introduction to Flex Layout Flex is the abbrev...
Everyone must be familiar with table. We often en...
Preface With the crazy speculation of virtual cur...
1. Demand A picture moves from left to right in a...
Unzip the file into a directory This is the direc...
The problem of resetting the password for Zabbix ...
I heard that there is an interview question: How ...
This article example shares the specific code of ...
Table of contents 0. Background 1. Installation 2...
Recently, the following effects need to be achiev...
[Solution 1: padding implementation] principle: I...
Table of contents Implementation effect diagram I...
This article shares the specific code of jQuery t...