0

Why is this JavaScript not getting rendered correctly when outputted as HTML?

var innerHtml = 
"<div style='color:yellow;font: 14px//14px /'Lucida Grande/',sans-serif;'>";

Am I not escaping something correctly?

2 Answers 2

4

/ should be \

var innerHtml = 
"<div style=\"color:yellow;font: 14px/14px 'Lucida Grande',sans-serif;\">"
Sign up to request clarification or add additional context in comments.

1 Comment

-1 You really weren't looking at what he was setting. font uses font-size/line-height, no backslashes necessary.
0

You don't need to escape forward slashes (/), nor do you need to escape single quotes (') (as you're quoting with double quotes "). The backslash is the escape character (\)

var innerHTML = '<div style="color:yellow; font: 14px/14px &quot;Lucida Grande&quot;, sans-serif;">';

should work. I used single quotes as I prefer to have HTML output use double quotes (not that it matters in JavaScript, but it's a consistency thing).

Edit to HTML escape the font-family quotes.

2 Comments

Shouldn't the font-face quotes be escaped since the same type of quotes are used for the entire style declaration?
@Sean Vieira yes, you are right I forgot to HTML encode the quotes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.