JavaScript string declarations from dynamic data with unescaped quotation marks -


i writing javascript templates content management system users fill out text input fields passed templates.

my problem quotation marks in input fields not escaped before passed template, have no way of knowing if contain single or double quotes, or both. whichever way try handle data code ends breaking because quotes terminate string declaration. want run function on data escape quotes can't find way data valid variable first.

is there way safely handle data in javascript without breaking string variable declaration?

edit: i'm posting code example;

cms text input field value is: who'll win our "big contest"?

text input field placeholder macro [%textinput%]

i'm building html template input, using js/html/css

<script> (function(){   var textinputstr = "[%textinput%]"; })(); </script> 

this break string declaration if value of textinput contains single quote, , vice versa.

this awesome question, , 1 deserves answer. strings in js don't have custom delimiter, in other modern languages, can stuck. 1 solution build script tag placeholder inside it, find innerhtml of tag , grab string variable. eg

<script id="parseme" type="text/template"> [%textinput%] </script> 

then use

var yourstring = document.getelementbyid("parseme").innerhtml 

now can manipulate string please.

hth!


Comments