Home > Article > Web Front-end > How to write JavaScript code in three different positions_javascript skills
The following is a list of three different places to write JavaScript code. The effect is to click the button button to pop up the alert warning box
The first one is the most common, the code is as follows
html code
js code
function pop()
{
alert("Called at JavaScript function");
}
The second is the simplest implementation method , the code is as follows
The third way is relatively complicated, the code is as follows
html code
js code
var obj=document.getElementById( "btn3");//The following statements must be placed below the definition of btn3, otherwise the compiler will not recognize btn3.
if(window.addEventListener)//Mozilla, Netscape, Firefox and other browsers
{
obj.addEventListener("click",fun,false);//Note the false here
}
else //IE browser
{
obj.attachEvent("onclick",fun);
}
function fun()
{
alert("By in function Trigger event in ");
}
Summary: The effect achieved by the three writing methods is exactly the same. It should be said that the three methods are commonly used, and each has its own advantages and disadvantages. . . .