skip to Main Content

Given:

<p id="copyright">
&copy;&nbsp;&apos;
<script>
document.getElementById('copyright').appendChild(document.createTextNode(new Date().getFullYear().toString().substr(-2)))
</script>
Company Name
</p>

How does one prevent the space between the ‘ and the two digit year, i.e.,
© ‘ 24 Company Name?

2

Answers


  1. Very simple, just remove the whitespace you put there (remember that the newline counts as whitespace here):

    <p id="copyright">
    &copy;&nbsp;&apos;<script>
    document.getElementById('copyright').appendChild(document.createTextNode(new Date().getFullYear().toString().substr(-2)))
    </script>
    Company Name
    </p>

    Indeed hacky though. Here is another approach:

    const copyrightYear = new Date().getFullYear().toString().substr(-2)
    document.querySelector('#copyright-year').textContent = copyrightYear
    <p id="copyright">
      &copy;&nbsp;&apos;<span id="copyright-year"></span>
      Company Name
    </p>

    Note that here, too, there must not be any whitespace between &apos; and <span id="copyright-year"></span>.

    Login or Signup to reply.
  2. There is hidden whitespace on the left of the script tag. You can fix it like this:

    <p id="copyright">
    &copy;&nbsp;&apos;<script>
    document.getElementById('copyright').appendChild(document.createTextNode(new Date().getFullYear().toString().substr(-2)))
    </script>
    Company Name
    </p>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search