JavaScript:重写alert()

发布于 2024-08-11 02:11:57 字数 133 浏览 4 评论 0原文

有没有人有过重写 JavaScript 中的 alert() 函数的经验?

  • 哪些浏览器支持这个?
  • 哪些浏览器版本支持此功能?
  • 重写该函数有什么危险?

Has anyone got any experience with overriding the alert() function in JavaScript?

  • Which browsers support this?
  • Which browser-versions support this?
  • What are the dangers in overriding the function?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

不弃不离 2024-08-18 02:11:57

绝对是“支持”的。这是您的网页,您可以用它做任何您想做的事情。

我已经这样做了,在不修改库的情况下通过潜入事件来跟踪分析事件。

使用代理模式:

(function(proxied) {
  window.alert = function() {
    // do something here
    return proxied.apply(this, arguments);
  };
})(window.alert);

如果需要(代理),您还可以绕过对原始函数的调用

更多信息:JQuery 类型#代理模式

It's definitely "supported". It is your web page, you do whatever you want to with it.

I already did this to track analytics events without modifying a library but by sneaking into events.

Use the proxy pattern:

(function(proxied) {
  window.alert = function() {
    // do something here
    return proxied.apply(this, arguments);
  };
})(window.alert);

You can also bypass the call to the original function if you want (proxied)

More info here: JQuery Types #Proxy Pattern

橘味果▽酱 2024-08-18 02:11:57

尽管大多数浏览器都支持覆盖它,但要小心您使用它所做的事情。

由于默认警报框会阻止执行线程,因此某些依赖此行为的库可能不再工作(最多)。

您应该成为一个好公民,避免接触本机 API。如果这样做,则在使用第 3 方代码时可能会破坏事情。

然而,如果您想在特定上下文中重新定义警报行为,您可以用匿名函数将其括起来,如下所示:

/* new funky alert */
function myFunkyAlert(msg) { 
    /* here goes your funky alert implementation */
    alert("Look ma!\n" + msg);
}

(function(alert) { // anonymous function redefining the "alert"

    /* sample code */
    alert("Hello World!");

})(myFunkyAlert);

Although most browsers support overriding it, be careful with what you're doing with it.

Since the default alert box blocks the execution thread, some libraries that rely on this behaviour might not work anymore (at best).

You should be a good citizen and avoid touching the native API. If you do, you could break things up, when using 3rd party code.

Yet, if you want to redefine the alert behaviour in a specific context, you could enclose it with an anonymous function, like this:

/* new funky alert */
function myFunkyAlert(msg) { 
    /* here goes your funky alert implementation */
    alert("Look ma!\n" + msg);
}

(function(alert) { // anonymous function redefining the "alert"

    /* sample code */
    alert("Hello World!");

})(myFunkyAlert);
染年凉城似染瑾 2024-08-18 02:11:57

超响报警功能不会产生任何危险。每个浏览器都支持它。

例如:

// function over riding. Redirecting to Console with Firebug installed.
function alert(message) { 
    console.info(message);
} 

alert('This is an override.');

There are no dangers in Overring alert function. Every browser supprts it.

for example:

// function over riding. Redirecting to Console with Firebug installed.
function alert(message) { 
    console.info(message);
} 

alert('This is an override.');
荭秂 2024-08-18 02:11:57

正如许多其他答案中所述,您可以使用

window.alert = null

window.alert = function(){}

覆盖该函数,但是,这不一定覆盖 Window 构造函数原型上的函数(请注意大写的 W< /code>),因此黑客仍然可以输入:

Window.prototype.alert.apply(window, ["You were hacked!"]);

因此,您还需要使用以下内容覆盖该函数:

Window.prototype.alert = null

Window.prototype.alert = function(){}

As said in many of the other answers, you can just override the function with

window.alert = null

or

window.alert = function(){}

however, this doesn't necessarily override the function on the prototype of the Window constructor (note the capital W), so the hacker can still type:

Window.prototype.alert.apply(window, ["You were hacked!"]);

therefore, you also need to override that function with:

Window.prototype.alert = null

or

Window.prototype.alert = function(){}
眉目亦如画i 2024-08-18 02:11:57

我认为每个 Javascript 实现都会支持这一点,并且不会有任何危险。通常将简单的操作系统风格的警报框替换为更优雅的 HTML/CSS。这样做意味着您不必更改现有代码!事实上,它是可能的,这使得 Javascript 变得很棒。

I think every Javascript implementation will support this, and there is no danger involved with it. It's commonly done to replace the plain OS-styled alert boxes to something more elegant with HTML/CSS. Doing it this way means you don't have to change existing code! The fact that it is possible makes Javascript awesome.

护你周全 2024-08-18 02:11:57

拉迪斯拉夫。
对于IE8,你可以像这样重新定义alert()

/** 
 * Definition of global attached to window properties <br/>
 */ 
    (function() {
      nalert = window.alert;
      Type = {
          native: 'native',
          custom: 'custom'
      };
    })();

/**
 * Factory method for calling alert(). 
 * It will be call a native alert() or a custom redefined alert() by a Type param.
 * This defeinition need for IE
 */ 
    (function(proxy) {

          proxy.alert = function () {
          var message = (!arguments[0]) ? 'null': arguments[0];
          var type = (!arguments[1]) ? '': arguments[1];

          if(type && type == 'native') {
           nalert(message);
          }
          else {
               document.write('<h1>I am redefiend alert()<br/>Alert say: '+message+'</h1>');
          }     
      };
   })(this);

并调用

alert('Hello, hacker!');
nalert('I am native alert');
alert('Hello, user!', Type.custom);

Ladislav.
For IE8 you can redefine alert() like this way

/** 
 * Definition of global attached to window properties <br/>
 */ 
    (function() {
      nalert = window.alert;
      Type = {
          native: 'native',
          custom: 'custom'
      };
    })();

/**
 * Factory method for calling alert(). 
 * It will be call a native alert() or a custom redefined alert() by a Type param.
 * This defeinition need for IE
 */ 
    (function(proxy) {

          proxy.alert = function () {
          var message = (!arguments[0]) ? 'null': arguments[0];
          var type = (!arguments[1]) ? '': arguments[1];

          if(type && type == 'native') {
           nalert(message);
          }
          else {
               document.write('<h1>I am redefiend alert()<br/>Alert say: '+message+'</h1>');
          }     
      };
   })(this);

and call as

alert('Hello, hacker!');
nalert('I am native alert');
alert('Hello, user!', Type.custom);
留蓝 2024-08-18 02:11:57

我对重写alert()函数的经验是,我们曾经用它来“破解”显示“请注册!”的JavaScript库的试用版。时不时地通过警报打扰屏幕。

我们刚刚定义了自己的alert()函数,瞧。

它仅用于测试目的,我们后来购买了完整版本,所以这里没有发生任何不道德的事情;-)

My experience with overriding alert() function is that we once used it to "hack" trial version of JavaScript library that displayed "Please register!" nag screen through alert time to time.

We just defined our own alert() function and voila.

It was for testing purposes only, we bought full version later, so nothing immoral going on here ;-)

可是我不能没有你 2024-08-18 02:11:57

现代浏览器中的所有 JavaScript 实现都支持覆盖。

危险很简单,你会通过覆盖众所周知的函数(例如alert())让其他团队成员彻底疯狂。

因此,除非您以某种方式重写函数作为工具来调试或破解现有代码,否则我认为没有任何理由这样做。只需创建一个新函数即可。

All JavaScript implementations in modern browsers support overriding.

The dangers are quite simply, that you would drive other team members absolutely crazy by overriding commonly known functions such as alert().

So unless you are overriding functions as a tool to perhaps debug or hack existing code in some way, I don't see any reason to do it. Just create a new function.

樱花落人离去 2024-08-18 02:11:57

我面临着显示默认消息以及实际警报消息的要求。这就是我设法做到的。


    const actualAlertFunc = window.alert;
    window.alert = function(msg) {
         actualAlertFunc('some default message '+ msg);
    }

我已经在 chrome 上测试过了。
我不确定这是否是一个好的做法,但它达到了目的。

I have faced a requirement to show a default message along with the actual alert messages. This is how I managed to do it.


    const actualAlertFunc = window.alert;
    window.alert = function(msg) {
         actualAlertFunc('some default message '+ msg);
    }

I have tested it on chrome.
I am not sure whether its a good practise, but it serves the purpose.

清音悠歌 2024-08-18 02:11:57

它确实可以在 firefox 和 ie8 中运行。我看不出有任何浏览器它不能工作。这几乎是 javascript 工作原理的基础,尽管人们不经常看到它与这样的本机函数一起使用 =)

It sure works in firefox and ie8. I can't see that there'd be any browser it wouldn't work in. This is pretty much fundamental of how javascript works, even though one don't often see it used with native functions like that =)

嘿嘿嘿 2024-08-18 02:11:57

说到 js 浏览器函数,window.alert 是最杰出、最知名的,不懂 js 的人都知道 alert() ——放心吧现在使用的所有浏览器都支持,您的代码片段也是如此。但是,我不会重写(这更像是重构,而不是 OOP 意义上的重写)alert() 对于您的特定用例,因为当您实际上需要使用 alert () 没有模板,您可能会这样做,那么您将需要另一个非警报函数来执行此操作。

When it comes to js browser functions window.alert is pre-eminent and most well known, people who don't know js know alert() -- rest assured it is supported in all browsers in use today and your code snippet is as well. However, I wouldn't override (well this is more like refactoring rather than override in the OOP sense) alert() for a particular use case as yours because when you actually need to use alert() with no template, and you probably will, then you'll need another non-alert function to do so.

小伙你站住 2024-08-18 02:11:57

我用来查找警报来源的一个快速方法是转到控制台,然后输入以下内容

function alert(message) { 
  console.info(message);
  debugger;
} 

A quick hack that I do to find where the alerts are coming from, is to go to console and then enter this

function alert(message) { 
  console.info(message);
  debugger;
} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文