How to overwrite the method in Javascript

Source: Internet
Author: User

A product I recently developed is embedded in Moss. However, since 2007, some JavaScript Functions of Microsoft have been written incorrectly or do not meet our requirements, we need to override these native functions.

For example, there is a native method called mtd1. to overwrite it, the main method is to use window. mtd1 = function {... your logic ....}; but sometimes for some purpose, the loading time of Microsoft JS files will be later than ours. In this way, we cannot normally overwrite this function. The solution to this problem is to use

Window. setTimeout (function ()
{
Window. mtd1 = function {... your logic ....};
},100 );

The above 100 is a time (in milliseconds). In actual use, it will be difficult to control the time. It will vary with network conditions and machine conditions, and it will also be too large to avoid execution too late.

There are still solutions. Let's take a look at the example.

Function setoverridefunction ()
{
If (window. mtd1! = NULL)
{
Window. mtd1 = function {... your logic ....};
}
Else
{
Window. setTimeout (function ()
{
Setoverridefunction ()
},100 );
}
}

Window. setTimeout (function ()
{
Setoverridefunction ();
},100 );

OK, perfect solution ....

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.