views:

36

answers:

2

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

+4  A: 

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

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
ShowModalDialog is available in Firefox since V3: https://developer.mozilla.org/en/DOM/window.showModalDialog
Pekka