31

I have the following statement:

btnApply.Attributes.Add("onclick", "alert('Must be a member to Use this Tool')");

Is there a way to add a new line. I tried, but it showed the in the text. In addition, can I add a link that would close the alert and take the user to another page?

How can I do this with my example?

When I added the \n, the alert doesn't show up.

Xaisoft
  • 45,655
  • 87
  • 279
  • 432
  • 1
    Your code uses the devil `eval()`.Instead, you should do something like `btnApply.Attributes.Add("onclick", function(){alert('Must be a member to Use this Tool')});` – Oriol Apr 21 '13 at 00:17

11 Answers11

65

You can't use HTML, but you can add line breaks:

alert('line1\nline2');

If a rich modal popup is really what you want though, you can use jQuery's dialog feature and put whatever you want in there.

Daniel Schaffer
  • 56,753
  • 31
  • 116
  • 165
15

Use \\n (needs two slashes).

You'll notice that before doing this the alert will not post even a javascript error. \n will not work because the slash needs to be escaped.

MadDawgg
  • 151
  • 1
  • 2
5

You cannot allow links but you can have newlines.

Just use "\n" for a newline instead of <br/>.

Jason Cohen
  • 81,399
  • 26
  • 107
  • 114
1

as Gumbo said, \n is working 100%. But don't forget to give spaces between text and the \n

Adam IS
  • 51
  • 5
1

Javascript has a specific AlertDialog that it opens when you you call alert('') and treats the entire body as text - far as I know there is no way of adding HTML to the displayed dialog.

I'd recommend using a Javascript library that supports a customizable dialog box or modal box like this Modal Box to get your desired behavior (and it will look better)

zaczap
  • 1,386
  • 1
  • 14
  • 20
1

You can't use markup in a javascript alert. However, you can achieve line breaks using "\n"

baretta
  • 7,385
  • 1
  • 25
  • 25
1

Alert is a fairly crude tool (and looks it too). Perhaps it's time to look at doing this in a more web friendly way...

Create a hidden message div on your page and instead of executing the alert, populate it with some message mark-up (including an anchor link if you wish) and reveal it to the user instead. You can add all kinds of nice visual tricks to this including fading in/out, centring, layering a semi-opaque background, etc, etc..

edit: jQuery dialog (as mentioned by others) is a nice way to handle this, most libraries will have some widget or another to do similar.

annakata
  • 74,572
  • 17
  • 113
  • 180
1

Short answer: you can't. Javascript dialogs are basic.

You can use VBScript's MsgBox method to create a custom dialog box but that'll only work on Internet Explorer with VBScript turned on. Not cross-platform; not recommended.

The alternative is to fake it in HTML, which a large number of Javascript libraries provide built-in functionality for.

Rob Grant
  • 7,239
  • 4
  • 41
  • 61
0

Maybe you can try confirm() instead alert(). This solution is not a link, but a redirection from button.

If you wrap the confirm into other var and check for true/false, you can perform 2 actions.

var r=confirm("Do you wanna do"); if (r==true) { window.location.assign('register.html'); // this will be append to current url. For other, use http:// full url assignement. } else { window.location.assign(''); // cancel does nothing }

m3nda
  • 1,986
  • 3
  • 32
  • 45
0

No, alert() only allows plain text. So \n is a line break.

Gumbo
  • 643,351
  • 109
  • 780
  • 844
0

You can use \n for a new line instead of br but remember if you are using alert in PHP echo then \n will not work for you. Your complete alert will be disappeared. For that, you have to use \n.