Is it really insecure to build HTML strings in Javascript?

前端 未结 4 664
走了就别回头了
走了就别回头了 2020-12-13 08:35

The company who hosts our site reviews our code before deploying - they\'ve recently told us this:

HTML strings should never be directly manipulated,

4条回答
  •  醉梦人生
    2020-12-13 09:02

    This should be just as secure, without compromising too much on readability:

    var link = $('Click to play');
    link.data("src", this.au);
    

    The point is to avoid doing string operations to build HTML strings. Note that in above, I used $() only to parse a constant string, which parses to a well known result. In this example, only the this.au part is dangerous because it may contain dynamically calculated values.

提交回复
热议问题