Jump to content

How Can I "escape" Apostrophes In Javascript


Recommended Posts

Simple script... checks a field value... if not blank, it ".writes" the value to HTML... if it is blank, it uses the data from a different field. If the variable value has an apostrophe in it, the whole thing returns a blank string. It obviously breaks the code. There are many reference to this type of thing on the web, but NONE of the solutions I've tried have worked. Below is a sample, including code to escape the apostrophe with a "\" character. Doesn't work...  HELP!

 

<script>

 

var X='[@field:Event_Name_Override]';

var Y='[@field:Event_Name]';

 

X = X.replace(/\'/g, "");

Y = Y.replace(/\'/g, "");

 

if (X.length>1)

{

document.write(X);

}

else

{

document.write(Y);

}

</script>

Link to comment
Share on other sites

  • 2 weeks later...

Simple script... checks a field value... if not blank, it ".writes" the value to HTML... if it is blank, it uses the data from a different field. If the variable value has an apostrophe in it, the whole thing returns a blank string. It obviously breaks the code. There are many reference to this type of thing on the web, but NONE of the solutions I've tried have worked. Below is a sample, including code to escape the apostrophe with a "\" character. Doesn't work...  HELP!

 

If your data can contain and apostrophe and double quotes, you can write value to some hidden field in HTML block, and read value in script later. It is more right way, because it will work in both cases. But if you just want avoid "apostrophe issue", you can use variant offered above.

Link to comment
Share on other sites

  • 10 years later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...