Why doesn't javascript newlines work inside html?

后端 未结 13 2546
梦谈多话
梦谈多话 2020-12-06 12:08

So I have the following:


  
    
  
<         


        
相关标签:
13条回答
  • 2020-12-06 12:43

    Actually your code is works. When you run it on browser console like Firefox, it will show you:

    <html><head></head><body>
        <script type="text/javascript">
          document.write('Hello\nWorld')
        </script>Hello
    World
    
    </body></html>
    

    Note on the Hello and World that is separated.

    But when displayed on HTML, line break (whitespace) will be ommited.

    To display "as is", you may surround the javascript with PRE tag, like:

    <html>
      <body>
        <pre>
        <script type="text/javascript">
          document.write('Hello\nWorld')
        </script>
        </pre>
      </body>
    </html>
    

    You will get your line break displayed on HTML.

    0 讨论(0)
  • 2020-12-06 12:47

    \n works, if you have a debugger of sorts (or similar developer tool) you can see the document source, and you will see that there is indeed a newline character. The problem is the way you are looking at the page - you're not reading it's source, you're reading it as an html document. Whitespace in html is compressed into a single space. So when you change the source, it does indeed change, although when interpreted as an html document, that change isn't shown.

    Your node.js error is most probably caused by the fact that you're running browser scripts on the server. I.e. scripts that refer to the document are intended to be run in a browser, where there is a DOM etc. Although a generic node process doesn't have such a global object because it isn't a browser. As such, when you try and run code that references a global object called document on the assumption that it exists just like in the browser, it will throw an error. document.write doesn't exist, if you want to write to the screen, try console.log or look into the other util functions.

    0 讨论(0)
  • 2020-12-06 12:47

    The document object represents an HTML document; any text written to the document will be processed by the browser's HTML renderer. In HTML, all adjacent whitespace, including line breaks (e.g., "\n"), are collapsed into a single space when rendered. This is why you need <br />, which is rendered as a line break. You can make \n work by replacing it with <br /> or by writing into a <pre> element:

    Hello
    World
    0 讨论(0)
  • 2020-12-06 12:49

    1) \n works in plain text files.

    2) <br /> works because you are doing HTML in string. Strings can hold characters without breaking the rest of the JS script.

    3) Not really. Perhaps when you are using <textarea>s.

    4) Most other \*'s

    5) More info needed.

    0 讨论(0)
  • 2020-12-06 12:49

    1,3. "\n" does work. If you do a document.write inside the body tag you can check document.body.innerHTML and see that the line break is indeed there.

    4.Anything HTML specific will be rendered HTML specific, so you will have to escape < and > into &lt; and &gt; for instance.

    5.document is an object available in browsers, it is not used in node since the DOM doesn't exist there. require("sys"); and use sys.print in nodejs.

    0 讨论(0)
  • 2020-12-06 12:50

    Why doesn't \n work?

    Because white space is just white space in HTML.

    Why does <br> even work?

    Because it is the HTML for a line break

    Shouldn't everything that's inside the script tags be strictly javascript instead of a dirty mix between html and js?

    That's subjective. document.write is considered dirty by many as well.

    You can always use createElement and createTextNode

    Is it possible to make \n work somehow?

    pre, white-space

    I know \t doesn't work either. Any other stuff that won't work inside html files?

    HTML is not plain text. Listing all the differences would be time consuming, and out of scope for StackOverflow. Try reading the specification.

    Unrelated question (I didn't want to open a new question just for this).

    It is completely unrelated. Open a new question.

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