Debug program using deleting method
Recently I am maintaining an in-house PHP content management system. There is an error on this page, but only for one record in database. Since the code works well for all other record, it should also work for this one. While I could not find the error, by checking the code line after line. Finally, I decide to delete some parts to find which line has problem. This page contains several sections, such as header, several container sections for ajax to load dynamically, footer, etc. So I just remove sections first, then I find which section has problem. After getting the wrong section, then I remove functions one after one. Finally, I get to know where the problem is.
The problem is JavaScript. There is a single quote ‘ in a person’s name, like O’lley. The programmer also used single quote in JavaScript, such as:
onclick='show_center("")'
When the page is loaded, the JavaScript becomes:
onclick='show_center("O'lley")'
The solution is to change the single quote ‘ to double quote ” as following:
onclick="show_center(\"\")"
In summary, Deleting is a way to find error in a page, and remember to consider this problem in your JavaScript code.
You may also like:
Leave a comment