JS position of writing
1, JS code can be written to the onclick attribute tag ↓
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head> <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> <title>Document</title> </head> <body> <!-- button是按钮标签 --> <= "Alert ( 'point me doing?')"onclickthe Button> Point me </ the Button > </ body > </ HTML >
When you click the "point me" button will pop up a warning box.
2, JS code can be written to a hyperlink href attribute, when you click a hyperlink, will perform JS code ↓
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head> <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> <title>Document</title> </head> <body> <a href="javascript:alert ( 'ha ha ha'); " </also point me>a> </body> </html>
When you click a hyperlink, it will pop up a warning box.
Although written in the tag attributes, but they belong to the structure and behavior of the coupling is not recommended.
3, JS code can be written in the script tag.
4, JS code can be written to external JS file and referenced by the script tag, recommended
First create a .js file ↓
This file is then introduced into the script tag ↓
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head> <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> <title>Document</title> <script type="text/javascript" src="/前端/36、JS/jswenjian/js外部引用.js"></script> </head> <body> </body> </html>
Note: Once the script tag to import external JS files, then the tag will not be able to write other JS code, wrote the browser will ignore, unless write a script tag, and then write the new script tag js code.