Set » HTML entity in JavaScript's document.title?

后端 未结 3 954
误落风尘
误落风尘 2021-02-05 16:52

I\'m setting document.title with JavaScript, and I can\'t find a way to supply » (») without it appearing as literal text.

Here\

相关标签:
3条回答
  • 2021-02-05 17:07

    Try

    document.title = 'Home \u00bb site.com';
    

    Generally you can look up your special character at a site like this and then, once you know the numeric code, you can construct a Unicode escape sequence in your JavaScript string. Here, that character is code 187, which is "bb" in hex. JavaScript Unicode escapes look like "\u" followed by 4 hex digits.

    0 讨论(0)
  • 2021-02-05 17:14

    document.title takes the string as it is, so you can do this:

    document.title = 'Home » site.com';
    

    If you need to provide it as the entity name, you can set the innerHTML attribute. Here are two examples of how you can do it.

    document.getElementsByTagName('title')[0].innerHTML = '»';
    // or
    document.querySelector('title').innerHTML = "»";
    
    0 讨论(0)
  • 2021-02-05 17:16

    Javascript does not use HTML entities.

    You should simply use the actual » character in your string, and make sure that the file is saved and sent as UTF8.

    0 讨论(0)
提交回复
热议问题