Disable page partial JavaScript method implementation

Source: Internet
Author: User
Method overrides, overriding the method to be disabled, and letting it do nothing, the results proved to be really feasible, but did not know that it was not a scientific method, I took it out and discussed it with you

The methods discussed in this article have not been used in the actual project, because I have not encountered such a need for projects, but the test found feasible.

I. source of my ideas

Javascipt is a good thing, its appearance makes the page page form more lively, of course, the advantage is not just these, and in recent years the booming AJAX application more people began to pay attention to javascipt small language (some of the program cattle do not think it as a language, most of the script title, Even look down on the application of the Scripting man. Now many blog official website open script permissions, allowing users to customize the script to enrich their own space, especially like some technical professional blog, provides a fairly loose development environment. But we have also found that some blogs will limit some scripting methods. Notice that I'm talking about a partial limitation, and if it's all a simple thing to do, just filter out the <script> script block, but how do you do it with some restrictions?

Because I did not encounter such a problem in the previous project, so did not do too much in-depth research, at the beginning just by feeling think of the "replacement" method. It is obvious that such a method will not work because it may be wrong. For example, to disable the alert method, the following code is available:

Copy Code code as follows:


window.alert (' Some message ');


Now, to make the above code invalid, just let alert change, such as converting it all to uppercase alert, which will certainly report the script error, but still can be used try{}catch{} to include alert, but this is a big problem for the identification of the ban-term package, And there will be a mistake: the alert in the document.write (' Alert some message ') is also replaced.

Then I thought of the method rewrite, rewrite the method to disable, and let it do nothing, the results proved to be really feasible, but do not know is not a scientific method, I took out to discuss with you.

Second, the concrete realization

Look at the following code to disable the "alert", "write" two methods:

Copy Code code as follows:


window.alert=function () {}


document.write=function () {}





window.alert (' Alert some message ');


document.write (' Write some message ');


It looks really simple, in the actual application, the first two lines out of the existence of an external JS file, and in the need to filter JavaScript method of the page to load the JS file (also can be in the user to edit the content block before loading this script, In this way, our Administrator or Web page author can still use the method that will be disabled in the previous HTML block, so it is not effective to invoke the disabled method afterwards.

Note: Finally, let me also disable some DOM manipulation methods, such as the Remove () method, because the user can use the DOM action method to remove the JS file you started loading.

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.