Coderer
Coderer

Reputation: 27314

How should I enter a Unicode character in CSS now that octal escapes are deprecated?

I am adding CSS styles using JavaScript (specifically with GreaseMonkey's GM_addStyle).

I'd like to put a Unicode character in a CSS property. I've seen a lot of questions like this one and the answer always seems to be along the lines of

#target:before {
    content: "\2611";
}

Now, as I said, this style is being specified in GM_addStyle, and the calling function has Strict mode enabled. When my script runs, I get an error on the console with the message octal literals and octal escape sequences are deprecated.

I think the conflict here is between doing the operation in JavaScript (i.e. putting a Unicode character into a JavaScript string) and the operation in CSS (escaping the character when declaring a CSS property). What syntax should I use to have the character escaped without generating an error?

Upvotes: 3

Views: 1073

Answers (1)

Paul S.
Paul S.

Reputation: 66404

I'm trying to do just this in GreaseMonkey, with Strict mode enabled.

If you're going via JavaScript, then the String '\2611' doesn't have the same meaning as you think it does and you probably want the String '\u2611' or '\\2611'

'\2611';  // "±1"
'\u2611'; // "☑"
'\\2611'; // "\2611"

Upvotes: 4

Related Questions