multiline alert not working

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Z1P2
    New Member
    • Sep 2007
    • 23

    multiline alert not working

    I have this alert that works:

    Code:
    <script type="text/javascript">
    var monitor = setTimeout("alert('The monitor is working!');",0);
    </script>
    </head>
    But I want to turn the alert message into a multiline alert, and when I do this:

    Code:
    <script type="text/javascript">
    var monitor = setTimeout("alert('The monitor is working!\n\ntest line two');",0);
    </script>
    </head>
    It doesn't work, any ideas what I'm missing here?

    -- PS the timeout is set to 0 for testing purposes only, otherwise it would have a value of 10000
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5388

    #2
    hi ...

    you have to escape the backslahes for the linefeed:

    [CODE=javascript]setTimeout("ale rt('The monitor is working!\\n\\nt est line two');",0)[/CODE]
    kind regards

    Comment

    • Z1P2
      New Member
      • Sep 2007
      • 23

      #3
      ohh, thank you very much. It works great now!

      Comment

      • dmjpro
        Top Contributor
        • Jan 2007
        • 2476

        #4
        Originally posted by gits
        hi ...

        you have to escape the backslahes for the linefeed:

        [CODE=javascript]setTimeout("ale rt('The monitor is working!\\n\\nt est line two');",0)[/CODE]
        kind regards
        Why should I put double slashes?
        Please let me know !

        Kind regards,
        Dmjpro.

        Comment

        • gits
          Recognized Expert Moderator Expert
          • May 2007
          • 5388

          #5
          the normal and better way would be:

          [CODE=javascript]setTimeout(func tion() { alert('test\nli ne2') }, 0)[/CODE]
          as you can see here we may use the single slash. but when using:

          [CODE=javascript]setTimeout("ale rt('test\nline2 ')", 0)[/CODE]
          javascript tries to evaluate the string and we get an unterminated string literal:

          Code:
          alert('test\n
          i think it evals it as a linefeed in our js-code ... so we have to escape the slash again:

          [CODE=javascript]setTimeout("ale rt('test\\nline 2')", 0)[/CODE]
          and now it is working.

          kind regards

          Comment

          Working...