Basic article: 1. JavaScript runs in HTML, how many references are there? What are the common output modes in--6.js?

Source: Internet
Author: User

The book connected above, the above mentioned a number of JavaScript basic knowledge, most of them are some conceptual things, in a serious and rigorous attitude, we have to take seriously, some items of the problem is a knowledge point of the periphery extension, in order to save space, together, If there is a description of the wrong place or a lack of description of the place, I hope you criticize correct, the following is I give me "reference answer", but only for reference:

1.JavaScript is running in HTML, how many references are there?

There are 3 ways I know:

The first: Externally referencing remote JavaScript files, such as <script type= "Text/javascript" src= ". /js/jquery-1.8.3.js "></script> (relative strength) or <script src=" http://common.cnblogs.com/script/jquery.js "type = "Text/javascript" ></script> (absolute road strength);

The second type: write directly on the page, such as:

<script type= "Text/javascript" > document.write (' Here is a line of JavaScript code 1 ');</script>

Third: Reference the external JS in JavaScript code:

<script> window.onload = function () {var script = document.createelement ("script");    Script.setattribute ("type", "Text/javascript");    SCRIPT.SRC = "Http://common.cnblogs.com/script/jquery.js";  document.getElementsByTagName ("Head") [0].appendchild (script); }</script>

  

Where is the 2.JavaScript usually written on the page?

There are roughly 3 places where JavaScript is written:

First place: Head, for example:

Second place: Any position of body, such as:

<body><script type= "Text/javascript" src= "Http://common.cnblogs.com/script/jquery.js" ></script ><p> here is a section of text! </p>    <script>    window.onload = function () {var script = document.createelement ("script"); scrip T.setattribute ("type", "Text/javascript"); script.src = "Http://common.cnblogs.com/script/jquery.js";  document.getElementsByTagName ("Head") [0].appendchild (script); }    </script></body>

Third place: In the form of an event written on the label, such as:

<p onclick= "Javascript:alert (' Call you, you point! ' > Point me! </p>

  

3. What are the ways to change the JS loading order?

JavaScript is loaded in order, different loading order can lead to different results, for example, a chestnut:

<script>    var txt = document.getElementById ("TXT"); alert (txt.innerhtml);    </script>    <div id= "txt" > I'm a piece of text, you can't find me! </div>        <div id= "val" > I am also a text, you can find me! </div>    <script>    var txt = document.getElementById ("Val"); alert (txt.innerhtml);    </script>

The first JS execution error, because the HTML code run is the first and the next load, the first JavaScript code executes before executing the following HTML code, so the DOM node is not found, will be error, if the HTML is executed first, in the running JavaScript code, Just like the second paragraph, you can run the success! Of course, I'm not saying that you must first execute the HTML code, in the execution of JavaScript code, this depends on the situation;

One more chestnut:

    <script>$ (". Calculator"). FancyBox ();    </script>       <script type= "Text/javascript" src= ". /js/jquery.fancybox.js "></script>

The above FancyBox plug-in if the loading order like above to load, it must be unable to execute, because the code execution depends on the JS library is executed later, the main content here is to explain, if it is in the use of a plug-in, dependent on a JS file, must be in front of the first run, Whether it's on head or body;

The above example shows that the different JS execution sequence, the execution of the content is not the same, so change the location of the JS file, you can change the loading order of JS (Note: This refers to a separate JavaScript file or fragment, does not include the order of the function).

What are the common values for 4.type properties? What does it mean?

Open Dreamweaver (I prefer to use this editor, get used to the hints he gave, don't say I'm low), hit a type on <script>, there are a lot of type attributes, we write him down and see what we have:

Application/ecmascript application/javascript application/x-ecmascript Application/x-javascript text/ecmascript Tex T/javascript text/jscript text/livescript text/tcl text/x-ecmascript text/x-javascript

oh,mygod! So much! You scared the baby!

Hit the <link>:

Text/css Text/javascript

Fortunately only 2!

The type attribute on the JS to represent the types of scripts, that is, MIME type, on the link to represent the MIME type of the overlay style sheet, commonly used is 2 (other not commonly used, do not know what the meaning of the use of the situation, which the great God Help Science):

Text/css (CSS text that tells the browser to load CSS styles)

Text/javascript (JavaScript text, tells the browser to load JS code)

Seems to explain a bit far-fetched, hehe!

5. What other properties are there besides the type attribute?

Open Dreamweaver again and give the tips to these few: ASYNC,DEFER,LANGUAGE,RUNAT,SRC

Async: Defines whether the script executes asynchronously, with the value: async;

Defer: Indicates that the script does not generate any document content, and the browser can continue to parse and draw the page. The meaning is that when the document is loaded and then execute this JS, if you want to learn more about this property, you can refer to here, if you English better, you can see here.

Language: Specifies the language of the script, which is now replaced with the type;

Runat: Its value is server, which means that it is running this code on the server, the client is not running, not used;

SRC: Refers to the reference path of an external script file

What are the common output modes in 6.js?

The output mode of JS is about the following types:

<div id= "Demo" ></div>    <script>//first    document.write ("<p> What are you doing, your mother told you to go home for dinner!") </p> ");///The second document.getElementById (" Demo "). InnerHTML =" I'll play again, my mother's rice is not ripe yet! " The third Kind, prompt box alert ("Who can call me to eat!") ");    </script>

In haste, there may be a place where the expression is wrong, and if found, hope can be criticized. Thank you! If you feel good, I hope you can recommend to students who are learning JS, greatly appreciated!

Basic article: 1. JavaScript runs in HTML, how many references are there? What are the common output modes in--6.js?

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.