0

Is it possible to change the text in a Javascript alert after it appeared?

For example would it be possible to create a timer inside of an alert?

┌────────────────────────┐
│ x seconds passed...    │
│                   <OK> │
└────────────────────────┘

...where x will be changed from 0 to passed seconds value.

I know that I can use a modal or something, but I want to know if it's possible to change the text from alert box after it appeared.

I guess it's not possible, but it's better to ask.

5
  • 1
    Nope :) (a bunch more text here just cause I have to to post this) Commented Aug 20, 2013 at 5:40
  • I don't think you can. But fwiw, you can always override window.alert and simulate an alert prompt with some dynamically generated HTML content. Commented Aug 20, 2013 at 5:41
  • Please provide official sources... Commented Aug 20, 2013 at 5:44
  • @Johnツ: No browser allows you to style or modify its native dialogs. There usually isn't a source for things that you cannot do. Commented Aug 20, 2013 at 5:45
  • Your guess is right. It's not possible. Try using a jquery dialog/modal for custom requirements. Commented Aug 20, 2013 at 7:08

6 Answers 6

2

After a bit of preliminary searching, it seems like it's impossible to do what you want. This is mainly due to security reasons, I think.

It is not recommended to use an alert box because (WebPlatform):

  • This is a synchronized method call. Meaning, calling this method pauses scripting execution of the window from which this method is called, until the user closes the displayed dialog. Also, depending on your cross tab/window usage, this can sometimes pause scripting executions of other windows/tabs from the same domain.
  • Calling this method in some browsers prevents the user from interacting with the entire browser, or browser window (along with all of its tabs), until the dialog is closed.
  • Intrusive dialog boxes are generally annoying for the user.

You could create your own dialog box using one of these:

You could always have an alert that says: "Hey, look here, user, I'm trying to tell you something!" and then use one of the above.


Another alternative would be to create a popup using JavaScript

window.open("http://example.com");

or

You could use showModelDialog or showModelessDialog

where you can use dialogArguments, dialogHeight, dialogWidth, dialogLeft, and dialogTop.

But the problem with that is that most people have popup blockers which would block your innocent popups.


An ActiveX only possibility seems to be the Modified Alert Box. I have not tested this as I do not have a Windows computer.


There also is nsIPromptService's alert and/or alertCheck. These seem to offer more functionality such as adding a title, or a check box. There are also prompts, selections, etc.

Unfortunately, this doesn't seem to be available in Javascript. It seems like it's only available for firefox extensions, etc (chrome users).

Sign up to request clarification or add additional context in comments.

Comments

1

I dont think so since it is a browser generated dialog box. And for obvious security reasons, we might not have access to it

Comments

1

Let's say "no". There just isn't the API to even start doing that, nor is there intended to be.

Also, JS is single-threaded & blocked during a window.alert() or confirm(). Seems pretty straight-forward that this is provably impossible, working within the JavaScript language.

Use a jQuery modal or whatever.

Comments

0

You can style a modal box that looks like browser alert, and manipulate it as you want. Browser components like alert boxes are not customizable.

Comments

0

Its not possible to customize alert. You can try Simple Modal dialog from YUI. You can customize it in your own way. There are also other 3rd party components available or you can make your own.

Comments

0

It is not possible to modify after it is displayed. If you want this at any cost go for custom alert box solutions.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.