tags:

views:

1292

answers:

7

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.

A: 

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

Gumbo
If I created an html page and I wanted to open that in the onclick event, how could I do that (what javascript function)?
Xaisoft
window.open() - Look it up, it has many configurable parameters.
Cerebrus
@Cerebrus, thanks! I didn't thought of that one! +1 for you :)
Cshift3iLike
+2  A: 

You cannot allow links but you can have newlines.

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

Jason Cohen
+11  A: 

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
Does the JQuery dialog accept asp.net?
Xaisoft
Not sure what you mean... jQuery is 100% clientside - so you can generate the content using ASP.NET and wrap the jQuery around it, if that's what you mean.
Daniel Schaffer
I meant, put c# code in there like a control.
Xaisoft
Not C#, but JavaScript.
Georg
I'm suspecting a fundamental misunderstanding of client-side code vs server-side code here...
Daniel Schaffer
You are probably right. I need to catch up on my client-side knowledge. Thanks for the help so far.
Xaisoft
When I put the \n in the text, the alert box doesn't popup, but when I remove it, it popups up.
Xaisoft
If the text is defined in server-side code, you'll need to escape the \. For example: literal1.Text = "Line1\\nLine2";
Daniel Schaffer
+1  A: 

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  A: 

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

baretta
+1  A: 

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
+1  A: 

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.

Robert Grant