search
HomeWeb Front-endJS TutorialA brief introduction to Javascript's external objects

A brief introduction to Javascripts external objects

Window browser:

- location:地址
- history:历史
- Document:文档
- screen:窗口
- navigator:帮助

> 1. The external object is the API provided by the browser -- * *BOM**

> 2. These objects are specified by w3c and designed and developed by browser developers

> 3. These objects are divided into 2 parts, among which BOM contains DOM

> 4. We can access these objects through js

# External objects

> BOM (Browser Object Model)

Browser Object Model , used to access and manipulate browser windows, is JavaScript that has the ability to talk to the browser.

> DOM (Document Object Model)

Document Object Model, used to operate documents.

##1. Dialog box

- alert(str)
- Prompt dialog box, displaying the content of str string

- confirm(str)
- Confirmation dialog box, display the content of str string
- Press the "OK" button to return true, other operations return false

>Case

//调用window对象的属性或方法,可以省略"window."
//1.弹出框
 //1)弹出框
 function f1(){
  alert("你好,小俊子");
 }
 //2)确认框
 function f2(){
  var v = confirm("你吃了吗?");
  //点击确定返回true,否则返回false
  console.log(v);
 }
 //3)输入框
 function f3(){
  var p = prompt("你吃的什么?");
  //点击取消返回null
  console.log(p);
 }

## 2. Timer

- Mostly used for dynamic clocks on web pages, making countdowns, and marquee effects

- Periodic clock

- With a certain Execute the code at intervals, in a loop

- setInterval(exp,time);

- Return the started timer object

- Stop the started timer

- clearInterval(tID)

- tID: started timer object

-One-time clock

- Execute code after a set time interval , instead of executing after the function is called

- setTimeout(exp,time);

- Stop the started timer

- clearTimeout(tID)

- tID: Started timer object

> Case

1) Periodic timer

//每隔N毫秒执行一次函数,反复执行,直到达到停止条件位置。
function f4(){
 var n = 5;
 //启动定时器,返回定时器的ID,用来停止定时器
 var id = setInterval(function(){
  console.log(n);
  switch(n%4){
   case 0: btn1();break;
   case 3: btn2();break;
   case 2: btn3();break;
   case 1: btn4();break;
   default: ;
  }
  n++;
 },100);
 //启动定时器就相当于启动了一个支线程,当前方法f4相当于主线程。
 //2个线程并发执行,不互相等待,
 //因此主线程在启动完支线程后立刻向下执行,而支线程却需要在1秒后才执行
 console.log("蹦");
}


2) One-time timer

//推迟N毫秒执行一次函数,执行完之后,自动停止,
 //也可以在未执行前手动停止
var id;
function f5(){
 //启动定时器,若想在未执行定时器前就将它停止,需要使用id
 id = setTimeout(function(){
  console.log("叮叮叮");
  f4();
 },3000);
}
function f6(){
 //若定时器已经执行,则取消无效; 若定时器还未执行,则可以取消
 clearTimeout(id);
 console.log("已停止!");
}

# 3. Common attributes

- screen object

- Contains information about the client's display screen
- Commonly used to obtain the resolution and color of the screen
- Common properties:
- width height
- availWidth availHeight

- History object

- Contains URLs visited by the user

- Length attribute: the number of URLs in the browser history list

- Method:

- back();
- forwird();

- location object

- Contains information about the current URL

- Commonly used to obtain and change the current browsing URL

- href attribute: URL address being browsed in the current window

- Method

- reload(): Reload the current URL, equivalent to refreshing

- navigator object

- Contains information about the browser

- Commonly used to obtain information about the client browser and operating system

> Case

//Location对象
 function f1(){
  var b = confirm("你真的要离开我吗?");
  if(b){
   location.href = "http://www.tmooc.cn";
  }
 }
 //刷新页面
 function f2(){
  location.reload();
 }
 //screen 对象: 获取屏幕宽高
 function f3(){
  console.log(screen.width);
  console.log(screen.height);
  console.log(screen.availWidth);
  console.log(screen.availHeight);
 }
 //history对象
 function f4(){
  history.forward();
 }
 //navigator对象
 function f5(){
  console.log(navigator.userAgent);
 }

## DOM

DOM operation


-Find node
-Read node information
- Modify node information
- Create node information

- Delete node

Read and modify

- Node information

- nodeName: node name

- nodeType: node type

- (1) Read node

- Read the name of the node, type

<p id="p1">1.<b>读写</b>节点</p>
<p id="p2">2.<b>查询</b>节点</p>
<p id="p3">3.<b>增删</b>节点</p>
var p1 = document.getElementById("p1");
console.log(p1.nodeName);
console.log(p1.nodeType);

- Read and write the content of the node

-The text in the middle of the double label is called content, and any double label has content

console.log(p1.innerHTML);
p1.innerHTML="1.<i>读写</i>节点";
console.log(p1.innerText);

- Read and write the value of the node


-The data in the form control is called the value. Only the following form controls have values:
                - input
                                                                                                                                 ‐ select

         ‐ textarea

The above is the entire content of this article. I hope that the content of this article can be of some help to everyone's study or work and if you have any questions, you can leave a message to communicate , and also hope to support PHP Chinese website!

For more articles related to Javascript’s external objects, please pay attention to the PHP Chinese website!


###
Statement
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
Python and JavaScript: Understanding the Strengths of EachPython and JavaScript: Understanding the Strengths of EachMay 06, 2025 am 12:15 AM

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScript's Core: Is It Built on C or C  ?JavaScript's Core: Is It Built on C or C ?May 05, 2025 am 12:07 AM

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript Applications: From Front-End to Back-EndJavaScript Applications: From Front-End to Back-EndMay 04, 2025 am 12:12 AM

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Python vs. JavaScript: Which Language Should You Learn?Python vs. JavaScript: Which Language Should You Learn?May 03, 2025 am 12:10 AM

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

JavaScript Frameworks: Powering Modern Web DevelopmentJavaScript Frameworks: Powering Modern Web DevelopmentMay 02, 2025 am 12:04 AM

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

The Relationship Between JavaScript, C  , and BrowsersThe Relationship Between JavaScript, C , and BrowsersMay 01, 2025 am 12:06 AM

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

Node.js Streams with TypeScriptNode.js Streams with TypeScriptApr 30, 2025 am 08:22 AM

Node.js excels at efficient I/O, largely thanks to streams. Streams process data incrementally, avoiding memory overload—ideal for large files, network tasks, and real-time applications. Combining streams with TypeScript's type safety creates a powe

Python vs. JavaScript: Performance and Efficiency ConsiderationsPython vs. JavaScript: Performance and Efficiency ConsiderationsApr 30, 2025 am 12:08 AM

The differences in performance and efficiency between Python and JavaScript are mainly reflected in: 1) As an interpreted language, Python runs slowly but has high development efficiency and is suitable for rapid prototype development; 2) JavaScript is limited to single thread in the browser, but multi-threading and asynchronous I/O can be used to improve performance in Node.js, and both have advantages in actual projects.

See all articles

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

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft