1

I'm working on an old site and i would avoid searching&replacing and i was wondering if it is possible to reaplce all alert massages function

alert('some msg');

with a jquery modal window...

i'm not asking how to do the modal window but if i can create a protoype to replace the function alert..

thanks

Francesco
  • 24,839
  • 29
  • 105
  • 152
  • possible duplicate of [JavaScript: Overriding alert()](http://stackoverflow.com/questions/1729501/javascript-overriding-alert) – Pekka Sep 18 '10 at 19:27
  • I don't think this is a duplicate, since the problem in this question (creating something modal that blocks in JavaScript) is quite different from that thread. – Matti Virkkunen Sep 18 '10 at 19:30
  • @Matti I think the OP is asking exactly for what the duplicate deals with but you have a point - a jQuery "modal" window won't be modal, so it will deviate differently from `alert()`'s native behaviour – Pekka Sep 18 '10 at 19:33
  • @Pekka: Judging by the post ("avoid searching&replacing") the thing he wants is something that works *identically* to the built-in `alert` function but looks different. I don't think the other thread has anything about that. – Matti Virkkunen Sep 18 '10 at 21:45

2 Answers2

4

You can override alert:

 window.alert = function(txt) {
    // Custom handler here
 }

Note that this will be non blocking though. Code after alert will execute without waiting.

vassilis
  • 1,385
  • 1
  • 10
  • 20
3

alert is special in the fact that it blocks until the user responds to the popup. Generally in JS this is a big no-no, because if you block, you'll block the entire user interface. This precludes using a jQuery modal popup as a direct replacement for alert. You need to modify the software to use callbacks instead of synchronous alerts. (Most of) JS is event-based and single-threaded, and there isn't really a way around that.

There is a function called showModalDialog which shows a modal popup window (the kind we all hate), but that's pretty much it. It originated from IE, but as Pekka pointed out it's also implemented in Firefox 3 or newer and based on a quick Google search Webkit seems to have some support for it as well. It's still not a neat in-page popup though.

Matti Virkkunen
  • 63,558
  • 9
  • 127
  • 159
  • 1
    ShowModalDialog is available in Firefox since V3: https://developer.mozilla.org/en/DOM/window.showModalDialog – Pekka Sep 18 '10 at 21:50