admin管理员组

文章数量:1299985

I've a .Net application wherein one of my pages, during initial page load, I'm setting the text of an asp label control. In my aspx page, I've a Javascript function to read the label text and show a confirmation pop-up message. The problem is, "\n" with in the label text is not introducing a line break in the JS confirmation message.

My Code Behind

lblMsg.Text = "string1" + "\n" + "string2"

Aspx Page

function PendingDeleteValidate() {
    var x = document.getElementById("<%=lblMsg.ClientID%>");

    if (confirm(x.innerHTML))
        return true;
    else
        return false;
}

The JS confirmation dialog message shows:

string1\nstring2

I need to show a line break between the two strings.

I've a .Net application wherein one of my pages, during initial page load, I'm setting the text of an asp label control. In my aspx page, I've a Javascript function to read the label text and show a confirmation pop-up message. The problem is, "\n" with in the label text is not introducing a line break in the JS confirmation message.

My Code Behind

lblMsg.Text = "string1" + "\n" + "string2"

Aspx Page

function PendingDeleteValidate() {
    var x = document.getElementById("<%=lblMsg.ClientID%>");

    if (confirm(x.innerHTML))
        return true;
    else
        return false;
}

The JS confirmation dialog message shows:

string1\nstring2

I need to show a line break between the two strings.

Share Improve this question edited Aug 29, 2020 at 22:21 Daniel Manta 6,71817 gold badges42 silver badges48 bronze badges asked Apr 30, 2013 at 19:19 Test_UserTest_User 1711 gold badge6 silver badges16 bronze badges 1
  • Does the confirmation dialog have to get its text from a label? The problem is you're using innerHTML and window.confirm doesn't know what to do with the HTML. It reads the \n as just two characters like any others. – Heretic Monkey Commented Apr 30, 2013 at 19:32
Add a ment  | 

3 Answers 3

Reset to default 5

Try "\r\n \r\n" after title text.

function myFunction() {
    confirm("Tittle of DialogBox \r\n \r\nMessage of dialogbox. bla..bla.. bla... bla... bla..");
}

Fiddle Demo

Set like this in code behind:

lblMsg.Text = "string1" + "<br/>" + "string2"

And this

if (confirm(x.innerHTML))

to this

if (confirm(x.innerText))

why not try this i always use it

\\n also it would be wise to replace \r with ""


like this , have fun

and if you are only showing alerts with javascript then here is my function:

StringBuilder sb = new StringBuilder();
sb.Append("alert('");
sb.Append(msg.Replace("\n", "\\n").Replace("\r", "").Replace("'", "\\'"));
sb.Append("');");
return sb.ToString();

see if it works your needs. Regards

本文标签: cNew line in Javascript confirmation messageStack Overflow