Ok it seems that lot’s of members at the forums is asking on how to display multi-line text in a JavaScript pop up box. So I decided to write this example so that other developers can reference it or if I encounter such a post in the forum then I can simply point them in this example.

 

Check the following code blocks below:

 

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title>Untitled Page</title>

    <script type="text/javascript" language="javascript">

        function ShowBoxWithConfirm()

        {

            var msg1 = "Message 1";

            var msg2 = "Message 2";

            var msg3 = "Message 3";

            if (confirm(msg1 + "\n" + msg2 + "\n" + msg3) == true)

            {return true;}

            else

            {return false;}

        }

        function ShowBox()

        {

            var msg1 = "Message 1";

            var msg2 = "Message 2";

            var msg3 = "Message 3";

            alert(msg1 + "\n" + msg2 + "\n" + msg3);

        }

    </script>

</head>

<body>

    <form id="form1" runat="server">

    <div>

        <asp:Button ID="Button1" runat="server" Text="Using Confirm" OnClientClick="return ShowBoxWithConfirm();" />

        <br />

        <asp:Button ID="Button2" runat="server" Text="Using Alert" OnClientClick="ShowBox();" />

    </div>

 

</form>

</body>

</html>

 

The trick there is we used, “\n” for creating a new line in a JavaScript. You can also use “\r” or “\r\n” for creating a new line in a set of strings.

 

Running the code above will show something like below in the page:

 

Using Confirm:

 

 

Using Alert:

 

 

That’s it! Hope you will find this example useful!

 

Technorati Tags: ,,