Home >Web Front-end >JS Tutorial >Examples of why not to use javascript inline_javascript techniques

Examples of why not to use javascript inline_javascript techniques

WBOY
WBOYOriginal
2016-05-16 16:51:461243browse

Many people have used Javascript like this

Copy code The code is as follows:

The above code is very convenient to use and is supported by all browsers

Although I am very early I have read some books, saying that it is easier to maintain if HTML, CSS, and JS are separated. However, I am greedy for convenience, so I often write like this. However, when I used Firefox to debug today, I found that it is better not to write inline, because it is very unsafe. , because using Firefox’s firebug can easily invalidate the code!

Let’s take a look at why:
Copy the code The code is as follows:

< ;html>

<script> <br>function al() <br>{ <br>alert("good"); <br>} <br></script>


Save



The above code is to click the label to pop up the dialog box

The effect is as shown below:
Examples of why not to use javascript inline_javascript techniques

But if I If you find the a tag in Firefox and remove the js code inside, the dialog box will not pop up when you click the a tag, as shown below
Examples of why not to use javascript inline_javascript techniques

So if your click event is It is impossible to judge whether the data entered by the user is legal.

I work in asp.net and I discovered this problem today because the asp.net server control has two click events, one in the front desk and one in the front desk. In the background, I used the click event in the foreground to judge the user's input data. If it was legal, I called the background event. As a result, I used Firefox to debug and discovered the phenomenon described above. Even if the data is illegal, the background method is called directly. Maybe I am not doing this. What kind of technology, but I am happy that I discovered this problem, and hereby post an article to share it, haha

Solution:

Give a tag an id, as shown below
Copy code The code is as follows:

Save
<script> <br>document.getElementById("a1").onclick=function(){} <br></script>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn