Disable some Javascript methods on the page

Source: Internet
Author: User

The methods discussed in this article have not been applied in actual projects, because I have not met projects that require such a purpose, but the experiment shows that it is feasible.

I. Sources of my ideas

The emergence of javascipt is a good thing that makes webpage Page forms more lively. Of course, the benefits are not just that. In recent years, the booming Ajax applications have made people pay more attention to the small language of javascipt (betterProgramNiu Ren does not regard it as a language. It is the title of script at most, and even looks down upon the application of script makers. 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> 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 section is available:Code:

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:

Window. Alert = Function (){}
Document. Write = Function () {}< P>

window. alert ( ' alert some message ' );
document. write ( ' write some message ' );

it looks really simple. In actual application, extract the first two lines separately 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: the last reminder is to disable some Dom operation methods, such as the remove () method, because you can use Dom operations 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.