Home > Article > Web Front-end > Disable JavaScript not all but part of the page_javascript tips
The method discussed in this article has not been applied in actual projects because I have not encountered a project with such a need, but the experiment found that it is feasible.
1. The source of my ideas
JavaScipt is a good thing. Its appearance makes web page expressions more lively. Of course, the benefits are not limited to these. In recent years, the booming AJAX applications have made people start to pay more attention to the JavaScipt small language (many programs are awesome) Think of it as a language, at most a script name, and even look down on the application of people who do scripts). Nowadays, many blog official websites open script permissions, allowing users to customize scripts to enrich their own space. Especially some technical and professional blogs provide a fairly relaxed development environment. But we also found that many blogs will restrict certain script methods. Note that what I am talking about here is partial restriction. If it is full restriction, it is very simple. Just filter out the 3f1c4e4b6b16bbbd69b2ee476dc4f83a script block. But how is partial restriction achieved?
Because I have not encountered such a problem in previous projects, I did not do too much in-depth research. At first, I just thought of using the "replacement" method based on my feeling. Obviously such an approach won't work because something could go wrong. For example, if I want to disable the alert method, I have the following code:
window.alert('Some message');
Now to make the above code invalid, just change the alert. For example, change it to uppercase ALERT. This will definitely cause a script error, but you can still use try{}catch{} to change the ALERT Included, but this is a big problem in identifying the ban package, and there will also be such an error: the alert in document.write('alert some message'); is also replaced.
Later I thought of method rewriting, rewriting the method to be disabled and making it do nothing. It turned out that it is really feasible, but I don’t know if it is a scientific method. I will discuss it with everyone. one time.
2. Specific implementation
First look at the following code to disable the "alert" and "write" methods:
window.alert=function(){} document.write=function(){} window.alert('Alert some message'); document.write('Write some message');
It seems really simple. In actual application, extract the first two lines separately and store them in an external JS file, and load this JS file first on the page where the JavaScript method needs to be filtered (you can also edit the content when the user Load this script in the previous line of the block, so that in the previous HTML block our administrator or web page creator can still use the method that will be disabled), so calling the disabled method later will not work.
Note: As a final reminder, you must also disable some DOM operation methods, such as the remove() method, because users can use DOM operation methods to remove the JS file you loaded at the beginning.