Customizes the original function of the window.

Source: Internet
Author: User

The problem was raised because of a recent project in which the design department designed the alert () interface to require development using this design. I had no idea that alert () could be customized, isn't it native? The native can change? But with the depth of study, I found that it is possible, below is my idea:

First we know that all global variables are properties of the Window object, all global functions are the methods of the Window object, and the execution of alert () can do this: Window.alert (); For the Window object, alert is the alert () The reference to the function, we re-set a reference to the alert to a custom function.

The code is as follows:

<script>Window.alert=function  () {    console.log ("custom Alert");} alert (); </script>

After execution the console really output "custom alert", so we can control the alert () method, do not achieve the desired result? Follow up and start coding yourself.

Customizes the original function of the window.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.