Free scripting and reconstruction
Keywords: script, reconstruction
Think about what is the like something like this, and suddenly I saw a post in the forum, gave an answer, and I didn't very cool, so I tried to rebuild a few times, write an experience.
The problem is like this: http://community.9cbs.net/expert/topic/3178/3178153.xml? Temp = .261532
Ask a regular expression, remove the IMG element of Name on the web page
I urgently use a regular expression, remove the IMG element of Name on the web page, where YYY is a variable, I want to make a function form
Funciton Qichu (IMG_NAME) {
/ / Remove the name attribute here IMG_name here, pay attention to IMG_NAME is a string variable
}
Why is why no regular reasons:
I know that the replacement of regular expressions is very convenient, but for HTML XML, this type of tagged document, access to the DOM's API should be orthodox, security (that is, can guarantee the Well FORMED) .
VAR count = document.all.tags ("img"). Length;
Also, see here, here has provided the ability to replace other labels.
In the post code, I will talk about the reconstructed view before posting: Reconstruction is a concept, spanning the boundaries of the language, Java needs to be reconstructed, C # need, Scripting is also required; Reconstructing garbage becomes artwork One means, we have a variety of garbage every day (maybe you tend to be a tone, do not make garbage, but this is ideal), the purpose is just to make the program to work, in these garbage, we pick a value (most Basic value, whether some people pay for this improvement, both value, of course, it seems to be a bit violating professional ethics, but often we need to face reality), turn it into art, some people say programming is art, but Note that not all people who work in art can become artists, they can make art.
Similarly, since I wrote a free script, I also talk about the script: I said the script refers to JavaScript (for other scripts, it can be called script, naturally there must be better than the program language), the script is a free , Flexible, lightweight programming language, many features in JavaScript are both cava, c #, etc., for example: dynamic type bindings (not very exact, weak type), dynamic add members, etc. The script has the basic object-oriented characteristics, not much, look at the topic.
Start: Solving the idea
Since the problem is distinguished according to the element's Name, it is necessary to get multiple, so do not use document.all.YYY to get Collection, because if this is necessary to determine the number of Name YYY If only one, it does not form a collection (IE and scripts are handling the characteristics of the web page elements, as a script charm), the judgment process will make the program difficult to understand, so the method of obtaining all IMG elements (not only one method), in order to prevent the Collection element Changes cause a loop error, first obtain a cycle value, define a temporary array, store an element instance of all IMG named YYY, and traverse the array clears the target IMG element, so there is the following code. VAR count = document.all.tags ("img"). Length;
VAR TMPARRAY = New Array ();
For (var i = 0; i { IF (document.all.tags ("img") [i] .name == "yyy") TMPARRAY [TMPARRAY.LENGTH] = Document.all.tags ("img") [i]; } For (var i = 0; i { TMPARRAY [I] .outerhtml = ""; } TMPARRAY = NULL; Perform the first reconstruction: extract method Extract the function and add a method to the Document.Body Dynamics (the charm of Script). The method is operated in the function. Since Document.Body has generated an instance, there is no need to operate directly to the protample, it can be added directly to the instance. . Note: The significance of the two RemoveElementsWithname before and after, the previous one represents a method of Document.Body, and the latter is a partial function. // // First reconstruction // Document.body.removeElementSwithName = Function RemoveElementsWithname (ElementTAG, ElementName) { Var count = document.all.tags (elementtag) .length; VAR TMPARRAY = New Array (); For (var i = 0; i { IF (DocumentTAG) [i] .Name == ElementName) TMPARRAY [TMPARRAY.LENGTH] = Document.all.tags (elementtag) [i]; } For (var i = 0; i { TMPARRAY [I] .outerhtml = ""; } TMPARRAY = NULL; } Document.body.removeElementSwithname ("IMG", "YYY"); Take the second reconstruction: Specifies the first reconstruction of the extracted code First focusing on the package, encapsulating the function (class), because RemoveElementSbyName is now a method of an object, so there is no meaning for access within the script, remove the name, use anonymous functions (The charm of the script) instead, and uses this to refer to Document inside the function, and transfer the method by document.body to Document, making the method more universal and change the function name with by. // // Second reconstruction // Document.removeelementsByname = function (ElementTag, ElementName) { Var count = this.all.tags (elementtag) .length VAR TMPARRAY = New Array (); For (var i = 0; i { IF (this.all.tags (elementtag) [i] .name == ElementName) TMPARRAY [TMPARRAY.LENGTH] = this.all.tags (elementtag) [i]; } For (var i = 0; i { TMPARRAY [I] .outerhtml = ""; } TMPARRAY = NULL; } Document.RemoveElementSbyName ("IMG", "YYY"); Completion: It's not finished. To this two reconstruction has been completed, it can be seen that there is still SMELLS in the code, such as RemoveElementSbyName, this long method name, temporarily let him.