覆盖 Javascript 警报功能 - 不适用于动态添加的 JS
Overrinding Javascript Alert Function - not working for dynamically added JS
我正在使用 Visual Studio 2010 来使用自定义模式覆盖 JS 警告框 box.I 我使用了以下内容:
(function() {
var proxied = window.alert;
window.alert = function(txt) {
//myFuntion(txt);
return proxied.apply(this, arguments);
};
})();
此 Js 函数导致显示我的自定义弹出框和警告框。来源 taken from here
而
window.alert = function(txt) {
//myFuntion(txt);
};
适用于 aspx 页面脚本。
问题是使用 RegisterStartupScript 和 RegisterClientScriptBlock 后面的代码动态添加的 Js 警报仍然显示本机警报 box.How 来解决这个问题。
您提供的第一个代码示例中的代理方法旨在向事件添加功能,而不是替换它。调用 alert 函数时会发生什么,它将调用您的函数,然后调用标准 window.alert 函数。 (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
};
})();
第二种方法适合您,因为它不使用代理,它完全替代了 window.alert 功能 (Codepen demonstration)
window.alert = function(txt) { // sets a new window.alert function
myfunction(); // calls your function
};
问题很可能是 RegisterStartupScript 和 RegisterClientScriptBlock 在您更改警报功能的脚本 运行 之前呈现它们的脚本。
我正在使用 Visual Studio 2010 来使用自定义模式覆盖 JS 警告框 box.I 我使用了以下内容:
(function() {
var proxied = window.alert;
window.alert = function(txt) {
//myFuntion(txt);
return proxied.apply(this, arguments);
};
})();
此 Js 函数导致显示我的自定义弹出框和警告框。来源 taken from here 而
window.alert = function(txt) {
//myFuntion(txt);
};
适用于 aspx 页面脚本。 问题是使用 RegisterStartupScript 和 RegisterClientScriptBlock 后面的代码动态添加的 Js 警报仍然显示本机警报 box.How 来解决这个问题。
您提供的第一个代码示例中的代理方法旨在向事件添加功能,而不是替换它。调用 alert 函数时会发生什么,它将调用您的函数,然后调用标准 window.alert 函数。 (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
};
})();
第二种方法适合您,因为它不使用代理,它完全替代了 window.alert 功能 (Codepen demonstration)
window.alert = function(txt) { // sets a new window.alert function
myfunction(); // calls your function
};
问题很可能是 RegisterStartupScript 和 RegisterClientScriptBlock 在您更改警报功能的脚本 运行 之前呈现它们的脚本。