2

We have JavaScript that writes to the document title and attempts to pad it with extra spaces...

document.title = "My Title  "

But this gets translated to "My Title  "

Anyone know how to prevent this?

1
  • I agree with SLaks, I see no reason why you should do this. If you just want to separate different elements of the title why not use document.title="My Title - My Subtitle"; etc. Commented Sep 20, 2010 at 15:03

3 Answers 3

8

  is an HTML entity reference that represents the character reference   (see list of entities in HTML 4) that again represents the Unicode character U+00A0. Now document.title is of the type DOMString that is similar to CDATA that’s value is not parsed. That means you cannot use HTML references for document.title as the value is not parsed as HTML.

Try Javascript’s Unicode escape sequences to denote these character like this:

document.title = "My Title\u00A0\u00A0"
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks so much, not only for the answer but for the detailed explanation.
1

The document.title property is a normal string that is not HTML-encoded.
You should set it to "My Title ".

However, the browser may strip the spaces itself; there is nothing you can do about that.

Comments

0

You can do this

const titleElement = document.getElementsByTagName('title')[0]
titleElement.innerHTML = ' '

But as pointed out, spaces of all types are stripped.

But, if you add ᠎ (the MONGOLIAN VOWEL SEPARATOR 😁) to the end, it will not strip out the previous spaces.

titleElement.innerHTML = '᠎ '

At least in Chrome.

Comments

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.