Home Web Front-end JS Tutorial Implement the composition of JavaScript (detailed interpretation of BOM and DOM)

Implement the composition of JavaScript (detailed interpretation of BOM and DOM)

May 21, 2018 pm 01:52 PM
javascript js Interpretation

Below I will bring you an article on how to implement JavaScript (detailed interpretation of BOM and DOM). Now I share it with you and give it as a reference.

We know that a complete JavaScript implementation needs to be composed of three parts: ECMAScript (core), BOM (Browser Object Model), and DOM (Document Object Model).

BOM:

BOM provides many objects for accessing browser functions that have nothing to do with the content of the web page (these are DOM ), currently, BOM has been moved into the HTML5 specification by W3C.

window object:

The core of BOM represents an instance of the browser. It is both an interface for accessing the browser window through javascript and a Global specified by ECMAScript. Object, which means any object, variable and function defined in the web page, has window as its Global object, and therefore has access to methods such as paresinInt(). (Excerpted from Height Three). In addition, if a web page contains frames, each frame has its own window object and is stored in the frames collection (starting at index 0, ltr, ttb).

First of all, the variables and functions in the global execution environment are all properties and methods of the window object. Of course, there is a slight difference between global variables and the directly defined window property type. Global variables (to be precise, explicitly declared global variables) cannot be deleted, but the window property can. In addition, there is another detail to note. Trying to access undeclared variables will cause an error, but there is no problem using the query window object.

So, what are the common properties or methods of window?

1.name, each window object has a name attribute, which contains the name of the frame. Usually to understand window relationships and frames.

2. Window position method: moveTo (x coordinate of new position, y coordinate of new position), moveBy (move x horizontally, move y vertically). These two methods are not applicable to the framework.

3. Window size attribute: innerWidth/Height (size of the view area (minus the width of the border)/* IE, Safari, firefox */), outerWidth/Height (returns the size of the browser window/* IE, Safari, firefox */). In Chrome, inner and outer both return the size of the view area.

Of course, you can change the window size through resizeTo (new window width, new window height), resizeBy (increase x than the original width, increase y than the original height). This love song method does not apply to frame structures.

4.window.open(URL, window target, attribute string, boolean whether the new page replaces the currently loaded page in the browser history) is used to navigate to a specific URL or open a new window . If a window target is specified and the window target is the name of an existing window or frame, the specified url will be loaded in the renamed window or frame. Otherwise, the new window that opens is named the target window. Of course, the keywords that can be specified for the window target include _self, _parent, _top, and _blank.

<a href=http://www.jb51.net>click me</a>
    <script>
    var link=document.getElementsByTagName("a")[0];
      alert(link.nodeName);   
     window.onload=function(){
      
      link.onclick=function () {
        window.open(link.href,"good","width=400px,height=300px");
        return false;
    
      }  
    }
  </script
Copy after login

The specific settings of the characteristic string here will not be described in detail. Those who are interested can click here

5. As a single-threaded language, js still allows setting the timeout value (specified Execute code after the event) and interval time value (cycle every specified time) to schedule code execution at a specific moment.

Timeout call: setTimeout (js code string, millisecond time). As a single-threaded language, the js task queue can only execute one piece of code at a time. If the task queue is empty after the set time interval, Then execute the code string, otherwise, wait until the previous code execution is completed before executing.

var al=setTimeout(function () {
      alert("good");
    },5000);
    alert(al); //2
Copy after login

Here, I called an anonymous function to output good after 5 seconds, and a warning box popped up in the window to display 2. It can be seen that the setTimeout() function returns a numerical ID, which is unique, then we You can use this ID to clear the timeout call, and you can use clearTimeout(ID) to clear it.

Indirect call: setInterval(), it accepts the same parameters as setTimeout(), and also returns a numerical ID, which can be cleared using clearTimeout().

6. System dialog box methods: alert(), confirm(), prompt(), etc. are written in my previous blog, click here

location object

Rather than being an object in the BOM, it is better to say that Location is an attribute in the window object. Of course, it is also an attribute of the document object in the DOM that will be discussed later, that is, window.location and document. location refers to the same object.

Location object attribute list. Modifying these attributes can load a new page and generate a new record in the history. Using location.replace() will not generate new records in the history.

##href"www.google.com"Returns the complete url of the current page, calling assign()pathnameportprotocolsearch

navigator object: The de facto standard used to identify browsers. Its properties and methods are mainly used to detect the type of browser.

The rest, such as the history object (save historical records) and the screen object (indicates the client's capabilities), are not useful in programming in js, so they will not be described in detail.

-------------------------------------------------- ----------------------------------

DOM:

DOM is an API based on XML and extended for HTML. DOM relies on node tree expansion.

First of all, we need to make it clear that the document node is the root node of each node. The document node has and has only one child node, which is the element html (document element).

Node type:

An interface in DOM1, implemented by all DOM node types (text nodes, attribute nodes, element nodes), this interface is used in js as Node type implementation.

nodeType attribute, owned by each node. Represented by 12 numerical values, element--1, attribute--2, text--3...

nodeName attribute. For element nodes, the value of nodeName is the label name.

nodeValue attribute, for element nodes, the value of nodeValue is null.

Node relationship: Each node has a childNodes attribute and saves a NodeList (like array object) object. Each node has a parentNode attribute that points to the parent node. The nodes in childNodes have the same parentNode. Sibling nodes can be accessed using the previousSibling and nextSibling properties. At the same time, childNodes[0]==firstChild, childNodes[childNodes.length-1]==lastChild.

Operation node: appendChild(), push a node to the end of NodeList, and return the newly added node. insertBefore(), unshifts a node to the head of NodeList and returns the newly added node. replaceChild(newChild,targetChild), replaces the target node. The original node is still in the document, but has no position. removeChild(tragetChild), removes nodes, has a similar effect to replaceChild(). cloneChild(boolean), true means complete copy (entire node and child nodes), false means basic copy.

Document type:

represents the document. The document object is an instance of HTMLDDocument (inherited from the Document type) and represents the entire html page. At the same time, the document object is also a property of the window object, so it can be accessed as a global object. document.firstChild==html. document.body==body. document.doctype--->Reference to . document.title--->title document.url--->location.url.

Find elements: getElementById(),getElementsByTagName(),getElementsByClassName().

Document writing Input: write(), writeln(), open(), close()

Element type:

getAttribute(), to get attributes for class, use " class", instead of className, you can get the class attribute when using element.className.

setAttribute(), set the attribute, and replace it if the attribute exists. Otherwise, create.

removeAttribute(), completely remove element attributes.

createElement(), creates a new element.

Text type:

createTextNode(), creates a text node. If two text nodes are adjacent sibling nodes, the two texts will be connected, no Space.

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

Basic specifications of JavaScript

Basic usage of Javascript

## Sample code for creating fireworks special effects using p5.js_javascriptTips

hash "#contents" Returns the hash in the url, There is no response for ""
##host "www.google.com" Return the server name and port number (if any)
hostname "www.google.com " Returns the server name without the port number
##''/wileyCDA/' Return directory name
"8080" Returns the port number, if not, returns an empty string
"http:" The protocol used by the return page
"?=javascript" Returns the query string, starting with a question mark

The above is the detailed content of Implement the composition of JavaScript (detailed interpretation of BOM and DOM). For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Recommended: Excellent JS open source face detection and recognition project Recommended: Excellent JS open source face detection and recognition project Apr 03, 2024 am 11:55 AM

Face detection and recognition technology is already a relatively mature and widely used technology. Currently, the most widely used Internet application language is JS. Implementing face detection and recognition on the Web front-end has advantages and disadvantages compared to back-end face recognition. Advantages include reducing network interaction and real-time recognition, which greatly shortens user waiting time and improves user experience; disadvantages include: being limited by model size, the accuracy is also limited. How to use js to implement face detection on the web? In order to implement face recognition on the Web, you need to be familiar with related programming languages ​​and technologies, such as JavaScript, HTML, CSS, WebRTC, etc. At the same time, you also need to master relevant computer vision and artificial intelligence technologies. It is worth noting that due to the design of the Web side

How to display file suffix under Win11 system? Detailed interpretation How to display file suffix under Win11 system? Detailed interpretation Mar 09, 2024 am 08:24 AM

How to display file suffix under Win11 system? Detailed explanation: In the Windows 11 operating system, the file suffix refers to the dot after the file name and the characters after it, which is used to indicate the type of file. By default, the Windows 11 system hides the suffix of the file, so that you can only see the name of the file in the file explorer but cannot intuitively understand the file type. However, for some users, displaying file suffixes is necessary because it helps them better identify file types and perform related operations.

Essential tools for stock analysis: Learn the steps to draw candle charts with PHP and JS Essential tools for stock analysis: Learn the steps to draw candle charts with PHP and JS Dec 17, 2023 pm 06:55 PM

Essential tools for stock analysis: Learn the steps to draw candle charts in PHP and JS. Specific code examples are required. With the rapid development of the Internet and technology, stock trading has become one of the important ways for many investors. Stock analysis is an important part of investor decision-making, and candle charts are widely used in technical analysis. Learning how to draw candle charts using PHP and JS will provide investors with more intuitive information to help them make better decisions. A candlestick chart is a technical chart that displays stock prices in the form of candlesticks. It shows the stock price

In which folder is the cookie data on your computer located? Detailed interpretation In which folder is the cookie data on your computer located? Detailed interpretation Jan 19, 2024 am 10:19 AM

With the continuous development of the Internet, people are increasingly inseparable from browsers. In browsers, everyone will use cookies more or less. However, many people don’t know which folder the cookie data is in. Let’s explain it in detail today. First, we need to understand what cookies are. Simply put, a cookie is a piece of text information stored by the browser, which is used to save some of the user's personal settings in the browser or record the user's historical operations, etc. When the user opens the same website again, c

PHP and JS Development Tips: Master the Method of Drawing Stock Candle Charts PHP and JS Development Tips: Master the Method of Drawing Stock Candle Charts Dec 18, 2023 pm 03:39 PM

With the rapid development of Internet finance, stock investment has become the choice of more and more people. In stock trading, candle charts are a commonly used technical analysis method. It can show the changing trend of stock prices and help investors make more accurate decisions. This article will introduce the development skills of PHP and JS, lead readers to understand how to draw stock candle charts, and provide specific code examples. 1. Understanding Stock Candle Charts Before introducing how to draw stock candle charts, we first need to understand what a candle chart is. Candlestick charts were developed by the Japanese

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

The relationship between js and vue The relationship between js and vue Mar 11, 2024 pm 05:21 PM

The relationship between js and vue: 1. JS as the cornerstone of Web development; 2. The rise of Vue.js as a front-end framework; 3. The complementary relationship between JS and Vue; 4. The practical application of JS and Vue.

How to get HTTP status code in JavaScript the easy way How to get HTTP status code in JavaScript the easy way Jan 05, 2024 pm 01:37 PM

Introduction to the method of obtaining HTTP status code in JavaScript: In front-end development, we often need to deal with the interaction with the back-end interface, and HTTP status code is a very important part of it. Understanding and obtaining HTTP status codes helps us better handle the data returned by the interface. This article will introduce how to use JavaScript to obtain HTTP status codes and provide specific code examples. 1. What is HTTP status code? HTTP status code means that when the browser initiates a request to the server, the service

See all articles