Dealing with JS strings that contain quotes
This page is getting a JavaScript error causing the comments element to not be displayed. Normally it would look like This.
Before being interpreted the script looks like this:
<script type="text/javascript">
if(<$BlogItemNumber$> >= 110883032941464400)
{
document.write("<div id=\"comments\">");
document.write("<BlogItemCommentsEnabled>");
document.write("<a name=\"comments\"></a>");
document.write("<p><strong>Comments:</strong></p>");
document.write("<BlogItemComments>");
document.write("<a name=\"<$BlogCommentNumber$>\"></a>");
document.write("<p>");
commentCounter++;
document.write(commentCounter + ") ");
document.write('<$BlogCommentAuthor$> @ <a href=\"#<$BlogCommentNumber$>\">
<$BlogCommentDateTime$></a>: <$BlogCommentDeleteIcon$>');
document.write("</p>");
document.write("<p class=\"commentBody\">");
document.write('<$BlogCommentBody$><br />');
document.write("</p>");
document.write("</BlogItemComments>");
document.write("<p><br /><a href=\"<$BlogItemCommentCreate$>\">Add a Comment</a></p>");
document.write("</BlogItemCommentsEnabled>");
document.write("</div> <!-- end comments -->");
}
else
{
document.write("<div class=\"postInfo\">");
document.write("[");
backBlog('<$BlogItemNumber$>','Daily Random');
document.write("]");
document.write("</div> <!-- end postInfo -->");
}
</script>
<$BlogCommentBody$> is a string that may contain either single or double quotes, or both, or neither. How I can write that string to the page without it getting messed up by the quotes? I don't have any control over how the input is gathered from the user, so I'm forced to deal directly with the string itself. Is there some way to replace the quotes w/ the ASCII values, or do some kind of
type thing to the string?
dk01 posted this at 02:32 — 2nd March 2005.
He has: 516 posts
Joined: Mar 2002
Can you use php?
If so see the php functions addslashes() and stripslashes().
In Javascript you can try using the escape and unescape functions.
Hope that helps.
-dk
IanD posted this at 02:36 — 2nd March 2005.
They have: 222 posts
Joined: Sep 1999
Can't use PHP, unfortunately
escape and unescape won't work because the quotes in the string would break the statement. e.g., escape("This sentance has a "double" quote"); or escape('This sentance has a 'single' quote');
Fighting for a Lost Cause.net
IanD posted this at 03:53 — 2nd March 2005.
They have: 222 posts
Joined: Sep 1999
If I could use PHP I wouldn't even need to mess with the quotes 'cause I could just do
<?php
(if postid > x)
{
plain html stuff
}
else
{
other html stuff
}
?>
Is there any way to do something like that in JavaScript? I tried but couldn't get it working.
Fighting for a Lost Cause.net
dk01 posted this at 04:04 — 2nd March 2005.
He has: 516 posts
Joined: Mar 2002
Ok now I see what you are doing. I know this way sounds really dumb but have you thought of just having it all in a hidden input feild? Its the only way I can see you getting out of the binds of JS.
Then you could just myVal = escape(formname.elementname.value);
-dk
IanD posted this at 23:53 — 3rd March 2005.
They have: 222 posts
Joined: Sep 1999
Actually, I'm just gonna go ahead and do everything in the if condition in HTML but inside a hidden element, and then if the condition is true I'll change the visibility of the element.
Fighting for a Lost Cause.net
IanD posted this at 00:07 — 4th March 2005.
They have: 222 posts
Joined: Sep 1999
It worked
Thanks for the inspiration dk01
dk01 posted this at 16:39 — 4th March 2005.
He has: 516 posts
Joined: Mar 2002
Glad I could help. Site looks great.
-dk
IanD posted this at 19:07 — 4th March 2005.
They have: 222 posts
Joined: Sep 1999
Thanks
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.