Jump to content
Sign in to follow this  
waltmayo

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>

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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...
Sign in to follow this  

×
×
  • Create New...