Path: csiph.com!weretis.net!feeder4.news.weretis.net!feeder5.news.weretis.net!news.solani.org!.POSTED!not-for-mail From: Thomas 'PointedEars' Lahn Newsgroups: comp.lang.javascript Subject: Re: How to deal with "messy" quotes? Date: Mon, 12 Dec 2016 21:19:50 +0100 Organization: PointedEars Software (PES) Lines: 83 Message-ID: <1671655.6tgchFWduM@PointedEars.de> References: Reply-To: Thomas 'PointedEars' Lahn Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8Bit X-Trace: solani.org 1481573991 11241 eJwFwQkBwDAIA0BLK4RnciAU/xJ6Z+rHGXBz2NoK41Nht/h08HSpFHJEA5Ebc5EXW0T/RdYDIpUR5A== (12 Dec 2016 20:19:51 GMT) X-Complaints-To: abuse@news.solani.org NNTP-Posting-Date: Mon, 12 Dec 2016 20:19:51 +0000 (UTC) User-Agent: KNode/4.14.2 Cancel-Lock: sha1:0xdW/4wWU/ZkM6UmnB53Khoi65s= X-Face: %i>XG-yXR'\"2P/C_aO%~;2o~?g0pPKmbOw^=NT`tprDEf++D.m7"}HW6.#=U:?2GGctkL,f89@H46O$ASoW&?s}.k+&. [2] [3] > var s = document.getElementById('nav'), > num = 0, > fn = " a new field name "; > /* in my actual code, the var s represent an element dynamically created > but to get key point across I'm using static element here > */ > s.innerHTML = " " + fn + " 'newFN(\""+fn+" + \", \"+num+\");'>"; The "field name" does not belong in the attribute value, and in a *single*- quoted string literal *double*-quotes do _not_ need to be escaped (and vice- versa). Because “num” and “fn” are global, they can be used as-is. However, the expanded value of “fn” needs to escaped to avoid code injection: s.innerHTML = " " + fn.replace(/&/g, "&").replace(/"; But if one insists that the expansions be in the attribute value, they must be escaped, too: String.prototype.escapeHTML = function () { return this .replace(/&/g, "&") .replace(/ | | Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.