<div id= "d1" ></div> <script > Function Createbutton () {var obj = document.createelement ("button") ; Obj.innerhtml= "point me!"; Obj.onclick=function () {//Processing click event} obj.onmouseover=function () {//Handling MouseOver Event} return obj;//here due to need Back to the object you created, so you cannot set obj directly to null. After return, obj is a local variable and cannot be externally disconnected from its reference to htmlelement. The problem leakage problem in IE will be the "var" button = Document.getelementsbyid ("D1"). AppendChild (Createbu Tton ()); button. do something (); button. do something (); ........ something. Certain things (buttons); ... </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]
This type of writing in IE 100% memory leaks
It's easy to solve some problems with try finally
Copy Code code as follows:
function Createbutton () {
var obj = document.createelement ("button");
Obj.innerhtml= "point me!";
Obj.onclick=function () {
Handling the Click event
}
Obj.onmouseover=function () {
Handling MouseOver Events
}
try{
return obj;
}finally{
obj = null;//This sentence is executed after return, effectively resolving the issue of the need to set the obj to null after
}
}
In a function or method, there are many places where the return value is required, and the last thing to do
=====================================================================
Attach one: JavaScript Error (try/catch/finally)
Introduction
Like other programming languages, JavaScript provides the possibility.
The try/catch/finally block. Usually when a error is encountered then the script stops and doesn ' t
Continue with the rest of the page. The try/catch/finally block can is used to continue the
Processing with the rest of the page. You just have to put the "code in your" try block and
An error in encountered there, then it'll call the catch block. The finally block is called always
Regardless of an error occurred or not. The following example makes the usage clear.
Example:
<ptml> <pead> </pead> <body> ; script type= "Text/javascript" > try {document.write (unknownvariable)} catch (e) {documen T.write (E.message + "")} finally {document.write ("This was the finally message.")} </script> </body> </ptml>