Skip to content

Overwrite native javascript function: confirm()

18. April 2012

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

Advertisements

From → JS

Leave a Comment

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: