_javascript Techniques for writing JavaScript three different location codes
Source: Internet
Author: User
The following list of three different places to write JavaScript code, the effect is to click Button buttons pop-up alert warning box
The first is the most common, with the following code
HTML code
<input type= "button" value= "1" id= "Btn1" onclick= "Pop ()" >
JS Code
function Pop ()
{
Alert ("Call at JavaScript function");
}
The second is the simplest way to implement the code as follows
<input type= "button" value= buttons 2 "id=" btn2 "onclick=" Javascript:alert (' direct write function '); " >
The third approach is relatively complex, with the following code
HTML code
<input type= "button" value= "3" id= "Btn3" >
JS Code
var Obj=document.getelementbyid ("Btn3");//The following statement must be placed under the definition 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 triggering an event in a function");
}
Summary: Three ways to achieve the effect of writing is exactly the same, it should be said that three kinds of methods are commonly used, and each has advantages and disadvantages ....
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service