Disable the specific implementation of some JavaScript methods on the page _ javascript tips-js tutorial

Source: Internet
Author: User
Method Rewriting: rewrite the method to be disabled and let it do nothing. The result proves to be true, but I don't know if it is a scientific method, I will discuss with you the methods discussed in this article. I have not applied them in actual projects, because I have not encountered any projects that need them yet, but the experiment has found that they are feasible.

I. Sources of my ideas

The appearance of JavaScipt is a good thing that makes webpage pages more lively. Of course, the benefits are not just that, in recent years, the booming AJAX applications have attracted more attention to the application of JavaScipt (some programmers do not regard it as a language, most of which are the title of a script, or even look down on people who engage in scripts. Currently, script permissions are available on the official websites of some blogs, allowing users to customize scripts to enrich their own space, especially some technical blogs that provide a quite loose development environment. However, we also find that some blogs impose restrictions on some script methods. Note: I am talking about some restrictions here. If it is all restrictions, it is very easy to simply filter out the script block, but how are some restrictions implemented?

I have not encountered such a problem in my previous project, so I have not done much in-depth research. At first, I just thought of using the "replace" method. Obviously, this method does not work because it may cause errors. For example, if I want to disable the alert method, the following code is available:

The Code is as follows:


Window. alert ('some message ');


Now we need to invalidate the above Code. Just change alert. For example, if we change it to uppercase ALERT, a script error will certainly be reported, however, you can still use try {} catch {} to include ALERT. However, this recognition of the banned language package is a major problem, and there will be such an error: the document. write ('alert some message'); alert is also replaced.

Later I thought of method rewriting, rewriting the method to be disabled, and letting it do nothing. The results prove to be true, but I don't know if it is a scientific method, I will discuss it with you.

II. Specific implementation

Let's take a look at the following code to disable the "alert" and "write" methods:

The Code is as follows:


Window. alert = function (){}
Document. write = function (){}

Window. alert ('alert some message ');
Document. write ('write some message ');


It looks really simple. In actual application, extract the first two lines and store them in an external JS file, load the JS file on the page where the JavaScript method needs to be filtered (you can also load the script in the first line of the content block that the user edits, in this way, our administrator or Web Page Maker can still use the method to be disabled in the previous HTML block. In this way, calling the disabled method will not work.

Note:Finally, I would like to remind you to disable some DOM operation methods, such as the remove () method, because you can use the DOM operation method to remove the JS file you loaded at the beginning.

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.