1

Is it possible to change a browser's default behaviour for built-in javascript methods?

For example:

Calling alert() from jQuery Mobile creates a non-themed dialog box. It would be great to override the browser's default alert() behavior and replace it with a slick jQuery Mobile themed dialog box?

I realize there are many options for creating themed dialog boxes in jQuery Mobile, but if it was possible to create a tiny javascript library that would intercept a call to alert(), that would be really nice.

Jack
  • 16,506
  • 19
  • 100
  • 167

3 Answers3

0
    Using this code you can override the default alert function in javascript and we can do actions based on the alert message. The default alert function will also work same with this.

(function () {
                var _alert = window.alert;
                window.alert = function (str) {
                    if (str == 'Invalid Sesssion')
                        window.location = "http://www.mywebsite.com/";
                    _alert(str);
                };
            })();
Prince Prasad
  • 1,528
  • 1
  • 16
  • 20
0

You can not style a JavaScript Alert, If you need something to style use a dialog. There should be plenty of examples if Googled.

Phill Pafford
  • 83,471
  • 91
  • 263
  • 383
  • Thanks. Is there really no way to override a built in function like alert()? Surely it must trigger an event that can be caught and prevented from continiuing the default behaviour? – Jack Feb 10 '12 at 21:42
  • I rephrased the question, to better explain what I am trying to achieve. It's curiosity more that necessity. – Jack Feb 13 '12 at 12:26
  • well I did see this: http://www.quora.com/Is-it-possible-to-style-the-JavaScript-pop-up-box-on-the-alert-function – Phill Pafford Feb 13 '12 at 13:58
  • 1
    Thanks. How do I gracefully close or delete this question because it the same as http://stackoverflow.com/questions/1729501/javascript-overriding-alert Apologies!!! Thanks for always helping Phill. – Jack Feb 14 '12 at 10:56
0

Check this jQueryMobile plugin for themed dialogs: http://dev.jtsage.com/jQM-SimpleDialog/index.html

Pablo
  • 2,834
  • 5
  • 25
  • 45
  • Thanks @Pablo, this is exactly what I used. I was just wandering if it was possible to override the browser's default behaviour.in response to alert() – Jack Feb 10 '12 at 21:38