Section 97th, using JavaScript

Source: Internet
Author: User

Using JavaScript

Learning Essentials:

1. Create an HTML page

2.<script> Label parsing

Some problems with 3.JS code embedding

A. Create an HTML page

Because JavaScript is nested within an HTML document, you want to create an HTML document

<!DOCTYPE HTML><HTMLLang= "en"><Head>    <MetaCharSet= "UTF-8">    <title>JavaScript tutorial</title></Head><Body></Body></HTML>

two. <Script> label parsing

<script>xxx</script> This set of tags is the main method for inserting JS into an HTML page. It mainly has the following properties:

1.charset: Optional. Represents the character set specified by the SRC attribute. Because most browsers ignore it, very few people use it. is to specify the character encoding for JS

Such as:

<type= "Text/javascript"  src= "1.js"  charset = "UTF-8" ></ Script >

2.defer: Optional. Indicates that the script can be deferred until the document is fully parsed and displayed before execution. Because most browsers do not support it, it is seldom used.

3.language: Deprecated. The original scripting language used for code. Because most browsers ignore it, don't use it.

4.SRC: Optional. Represents the external file that contains the code to execute.

Such as:

<type= "Text/javascript"  src= "1.js"  charset= " UTF-8 "></script>

5.type: Required. Can be seen as a substitute for language. Represents the content type of the scripting language that the code uses.

Write an example in the HTML document: Type= "Text/javascript".

<script type= "Text/javascript" >    alert (' Welcome to JavaScript world! '); </script>
<!DOCTYPE HTML><HTMLLang= "en"><Head>    <MetaCharSet= "UTF-8">    <title>JavaScript tutorial</title>    <Scripttype= "Text/javascript">Alert ('Welcome to JavaScript World! ')    </Script></Head><Body></Body></HTML>

three. Some problems with JS code embedding

If you want to pop up a string of </script> tags, then the browser will misunderstand that the JS code is over. The solution is to divide the string into two parts, connected by a connector ' + '.

<type= "Text/javascript">    alert ('</ SCR'+'ipt>'); </ Script >

In general, when the JS code is more and more large, we'd better save him as a. js file, which can be introduced via SRC. It also features high maintenance, cacheable (load once, no loading), and easy to expand in the future

<type= "Text/javascript"  src= "Demo1.js"></ Script >

So there is no JS code inside the tag. However, it is important to note that, although there is no code, you cannot use a single label:

<type= "Text/javascript"  src= "demo1.js"/>; 

Nor can you add any code inside it:

<type= "Text/javascript"  src= "Demo1.js">  Alert (' I'm pathetic, can't do it!  ')</script>

As a general rule, we will store the <script> tags between

<Head>    <MetaCharSet= "UTF-8">    <title>JavaScript tutorial</title>    <Scripttype= "Text/javascript">Alert ('Welcome to JavaScript World! ')    </Script></Head>

Smooth degradation does not support JavaScript processing: <NOSCIPRT> Now the browser is supported, but some browsers turn off the JS support feature, we can use the <nosciprt> tag to give hints

<!DOCTYPE HTML><HTMLLang= "en"><Head>    <MetaCharSet= "UTF-8">    <title>JavaScript tutorial</title>    <Scripttype= "Text/javascript">Alert ('Welcome to JavaScript World! ')    </Script></Head><NoScript>JavaScript support must be enabled on this site, please open the browser support JavaScript</NoScript>Hello<Body></Body></HTML>

Section 97th, using JavaScript

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.