Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the JavaScript development techniques?

Shulou Source: shulou.com Published: 2022-06-02 12:46:11 09月13日 Update

This article introduces the relevant knowledge of "what are the JavaScript development technologies?". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. Use the correct label

If you need to use some JavaScript code in an html document, you should usually use the following tags:

/ / insert your code here

But in the source code, the following tag writing can be seen everywhere:

/ / insert your code here

In HTML, the language attribute is obsolete because with the type attribute, language it is redundant. Don't write like that again.

In fact, the client defaults to specifying the type= "text/javascript" attribute for the JavaScript code, and there is no need to write the type attribute unless a type such as type= "application/x-javascript" is required. Here is a reference document about JavaScript MIME types: http://krijnhoetmer.nl/stuff/javascript/mime-types/

2. Place the code in an external file

Using external .js documents is simpler than writing JavaScript code directly in html documents, and it also allows JS files to be cached to ensure quick access to website resources.

Put your JavaScript code in a .js document, and then use tags in the html document to introduce it:

3. Avoid using HTML comments to package code

Some very old browsers in the 1990s could not execute JavaScript code. To prevent these browsers from producing unnecessary results. Between 1994 and 1997, wrapping JavaScript code with comments in HTML was a good compatibility solution to ensure that browsers that did not support JavaScript could ignore it.

Here is a simple example:

In 2010, however, all browsers (even the lovely IE 6) could interpret JavaScript code, so there was absolutely no need to wrap JS code with comments. To make matters worse, if the code is wrapped in HTML comments and uses the-symbol, the browser may mistakenly think that the HTML document is over.

4. Use the framework

Unless your JavaScript code is short or simple, you should use the framework to avoid too much code duplication. In my opinion, jQuery is a * and has a great community, so it's worth trying.

Mango language: in fact, YUI is also very good, systematic, powerful, perfect and secure. There is a more forked Loader mechanism to improve the performance of Web applications more efficiently.

5. Declare variables using the var keyword

You should use the var statement to declare the variable, otherwise the variable will exist in the global scope and use var to make the code readable.

For example, the following case:

Var name = "Jean"; var size = data.length

There was also a variable scope issue in previous comments on the object reference mechanism in JavaScript.

6. Keep the code separate

A few years ago, when a programmer wanted to add events to a HTML element (for example, you want to validate time information as the user typed), he would use special attributes to place JavaScript code in html, such as onblur, onchange,onclick, and so on. For example:

Tags: Code documents properties files browsers browsers tags content packages variables comments scripts development cases questions pages development technology technology simplicity necessity Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Microsoft NVidia macOS Shulou Tech Info