-2

I'm am using Visual Studio 2010 to override JS alert box with a custom modal box.I've used following :

(function() {
  var proxied = window.alert;
  window.alert = function(txt) {
  //myFuntion(txt);
  return proxied.apply(this, arguments);
  };
})();

this Js function is causing both my custom pop up & alert box to be displayed. Source taken from here while

  window.alert = function(txt) {
  //myFuntion(txt);
  };

works just fine for aspx page scripts. The problem is Js alert dynamically added by code behind using RegisterStartupScript and RegisterClientScriptBlock are still showing native alert box.How to resolve this.

Community
  • 1
  • 1
Chitransh
  • 1
  • 2
  • Why are you using the proxy method? That is going to call your function and then call window.alert. That method won't replace the alert function it just adds to it. – Jacob Petersen Aug 27 '15 at 18:37
  • I realized my mistake later but didn't know how to close the question. – Chitransh Aug 29 '15 at 14:58

1 Answers1

0

The proxy method in the first code sample you provided is designed to add functionality to an event, not to replace it. What happens is when the alert function is called, it will call your function and then call the standard window.alert function. (Codepen demonstration)

(function() {
  var proxied = window.alert; // stores the original window.alert function
  window.alert = function(txt) { // sets a new window.alert
  myfunction(); // calls your function
  return proxied.apply(this, arguments); // calls the original window.alert function
};
})();

The second method works for you because it does not use a proxy, it completely replaces the window.alert function (Codepen demonstration)

window.alert = function(txt) { // sets a new window.alert function
  myfunction(); // calls your function
};

The problem is most likely that RegisterStartupScript and RegisterClientScriptBlock are rendering their scripts before your script that changes the alert function is run.

Jacob Petersen
  • 1,463
  • 1
  • 9
  • 17
  • adding `myFuntion(txt);` in master page above contentplaceholder solves the issue of properly displaying modified alert even when added dynamically – Chitransh Aug 29 '15 at 15:03