Why HTML symbol for ▶ does not work in document.title

I tried using ► when setting document.title , but it didn’t display the play character, it just showed the same characters.

I can insert ▶ directly into the code that works. I'm just wondering why the first method will not work.

Thanks!

+4
source share
2 answers

Another answer is correct, since the title of the HTML document is plain text and cannot contain markup.

However, to be clear, you can use &#9658; in the HTML source (i.e. <title>&#9658;</title> ). The parser converts the object to the corresponding character in the string, which is stored in the DOM.

You are having trouble installing document.title . The HTML parser does not apply to JavaScript, even if the script is embedded in an HTML document. Your script simply sets the title of the text text of the document to the literal ampersand-pound-9658 semicolon, because this sequence of characters does not really matter in the script.

If you want to escape the character in the script source, use a JavaScript escape sequence . Unicode escape sequences in JavaScript require a hexadecimal representation of a Unicode code point, and your entity uses a decimal representation (9568). Convert to hex and get 25BA:

 document.title = '\u25ba Playing'; 
+6
source

&#9658; It is a reference to an HTML object, but document.title does not contain HTML; it contains plain text. For example, if I set document.title to <strong>Hello</strong> <em>world</em> , it would display it literally and not show the Hello world. The same goes for any other part of HTML, including entity references.

To be clear, you can include entity links in the title tag. They will be interpreted when analyzing the page. Nevertheless, document.title gets access to the text after its analysis and replaces all entities. document.title will access the title tag, not the HTML inside it, which was originally used to create it. Similarly, setting document.title does not make the HTML inside it automatically interpreted.

Including a character literally can cause problems if the browser for some reason interprets your page in a different character encoding than it actually is. HTML objects protect against this in HTML, but there is also a way to avoid them in JavaScript. In particular, you can include ► in JavaScript with the escape code of the string \u25ba . (9658 10 converted to base 16 is 26BA 16 )

+8
source

Source: https://habr.com/ru/post/1484554/


All Articles