Overwrite native javascript function: confirm()

Task

You want to overwrite the native javascript function “confirm()”

Howto?

Create a javascript function and include it in your html page

function confirm(msg, callback) {
 ...
 }

Hint

Don’t do it like this:

window.confirm = function(msg, callback) {
 ...
 }

Why? Because IE will not allow overwriting of native functions. (Only tested with IE9)

Important

  • You will loose the typical alert()/confirm() behaviour, where all events in the browser are stopped.
  • In other words: The confirm() dialogs don’t exclude each other any more. They are displayed at the same time, this is due to the asynchronus nature of javascript.
  • Workarounds:
    • Use callback functions in your dialog handlers
    • Use the “Ok”-Button of your dialog handler to call subsequent dialogs

Links

http://blog.vawterconsultingservices.com/index.cfm/2011/11/16/Overriding-OS-alerts-and-confirms-using-jQuery

http://stackoverflow.com/a/437118

http://dbushell.com/2012/02/13/are-you-sure-the-user-experience-of-confirmation-dialogs/

http://stackoverflow.com/questions/1135284/overwriting-native-methods-in-javascript