Example why not use javascript inline

  • 2020-03-30 02:40:41
  • OfStack

A lot of people have used Javascript this way
 
<a href="#" onclick="al()"> save </a> 

The above code, very convenient to use, all browsers support

Although I have read some books very early, said that HTML, CSS, js separated, easier to maintain, but I covet, or often write so, but I found today when debugging with firefox, or do not write lines, because it is not safe, because with firefox firebug can easily let the code invalid!

Here's why:
 
<html> 
<head> 
<script> 
function al() 
{ 
alert("good"); 
} 
</script> 
</head> 
<body> 
<a href="#" onclick="al()"> save </a> 
</body> 
</html> 

The code above is a TAB popup dialog box

The effect is as follows:
< img SRC = "border = 0 / / files.jb51.net/file_images/article/201404/201404181133273.gif? 2014318113339 ">  

But if I had found the a TAB in firefox and removed the js code, clicking on the a TAB would have stopped the dialog, as shown below
< img SRC = "border = 0 / / files.jb51.net/file_images/article/201404/201404181134144.gif? 2014318113426 ">  

So if your click event is to determine whether the data entered by the user is valid, then there is no way to determine

Asp.net I do, do it today found this problem, because the asp.net server control, there are two click event, a front desk a background, I use the click event of the front desk user input data, legal event called the background, the results using firefox debugging, it discovered the phenomenon mentioned above, even if the data is also illegal to directly call the background method, maybe I this is not what technology, but I found this problem for yourself, hereby send articles to share, ha ha

Solutions:

Label a with an id, as shown below
 
<a id="a1" href="#"> save </a> 
<script> 
document.getElementById("a1").onclick=function(){} 
</script> 

Related articles: