JavaScript code placement location for javascript program development, JavaScript Program Development

Source: Internet
Author: User

JavaScript code placement location for javascript program development, JavaScript Program Development

When JavaScript is used on pages, where should these JS Code be placed? Let's take a look.

Generally, there are two ways to write on the interface and use. js files.

1.1 Head part on the Interface

You can directly put it in the head label, as shown in the following code:

1.2 body section on the Interface

Generally, it is directly placed in the body, as shown below:

There is no difference between head and body. Generally, when there is not much code, and only the current page uses these js, you can directly write it on the interface.

2. JS files

For JavaScript code that is complex and has a large amount of code, it is best to put it in a special. JS file and reference it on the page according to the relative path of the js file.

The advantage is that it can prevent a lot of duplicate js Code. Some common js methods can be put in external js files.

For example, general use visual studio 2010 New asp.net project with jquery-1.4.1.js files, we see how to use this js file.
For example, the file structure of the page,


You need to use this js file in MyJSFrm. aspx to introduce it like this.

In short, do not forget to use the relative directory. If the directory level of the current page file is relatively deep, use ../to calculate the directory level by yourself.

Differences between three js locations:

Head:

-- The script has been loaded when it is called.
--

Body:

-- The page is loaded when the page is generated.
--

External js:

-- Reference external js Note: External js cannot contain the <script> </script> labels
-- This mainly aims to save the cost of embedding the same js Code in each page when multiple pages call the same js function repeatedly;

The browser does not recognize js. Solution: <! --
Code Section
// -->

Articles you may be interested in:
  • How to get the mouse position in js. Multi-browser compatibility code
  • Insert a row at a specified position in the JavaScript talbe table
  • Code for retrieving the location of page elements using Javascript
  • Use javascript to obtain the code of the object where the cursor is placed on the page and the event is triggered
  • JavaScript code compatible with multiple browsers for retrieving elements and mouse positions
  • Javascript code for obtaining the mouse position in IE or Firefox
  • Javascript code used to obtain the page height and the position of the scroll bar
  • Detailed code for obtaining the geographic location of website visitor IP addresses using APIs (JavaScript Interface) provided by google
  • Code for js and jquery to obtain the x and y coordinates of the current mouse
  • Code for finding the relative position of a child element in the parent element in Javascript
  • Js Implementation ideas and code for getting the place where the mouse clicks
  • Js implementation code for retrieving the relative window position of Elements
  • Where should JavaScript code be placed in HTML code?

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.