BOM operation knowledge points in JS
window object
Global variables and global methods are both Return to window
alert-comfirm-prompt
Let the prompt text on alert, confirm and other pop-up boxes realize line wrapping:\n
// confirm() // 点击确定返回true,取消返回false var btn=document.getElementById("btn"); btn.onclick=function(){ // 弹出确认对话框 var result=window.confirm("您确定要删除吗?删除之后该信息\n将不可恢复!"); if(result){ document.getElementById("box").style.display="none"; } } // prompt("text","defaultText") // text:对话框中显示的纯文本 // defaultText:默认的输入文本 // 点击确认返回文本,点击取消返回null var message=prompt("请输入您的星座","天蝎座"); console.log(message);
open-close
If the url parameter in the open method is empty, the new window will also be opened but no document will be displayed
window.onload = function(){ // 打开子窗口,显示 newwindow.html window.open("newwindow.html","newwindow","width=400,height=200, left=0,top=0,toolbar=no,menubar=no,scrollbars=no,location= no,status=no"); var quit = document.getElementById("quit"); // 点击关闭当前窗口 quit.onclick = function(){ window.close("newwindow.html"); } }
Delayed call to setTimeout()
//调用函数 var fnCall=function(){ alert("world"); } setTimeout(fnCall,5000); //调用匿名函数 var timeout1=setTimeout(function(){ alert("hello"); },2000) clearTimeout(timeout1);
Achieve the following requirements:
(1) 3 seconds after clicking the "Delete" button, the text in p on the page disappears
(2) After clicking the "Delete" button Within 3 seconds, if you click the "Cancel Delete" button, the text in p on the page will not be deleted
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title>定时器</title> <style type="text/css"> p{width:400px;height:120px;margin-top:50px;border:2px solid gray;padding:10px;} </style> </head> <body> <input type="button" value="删除"> <input type="button" value="取消删除"> <p>点击"删除"按钮后,里面的内容将在3秒钟后消失; <br/><br/>如点击了"删除"后又不想删除内容,请在点击"删除"按钮3秒之内点击"取消删除"按钮即可</p> <script type="text/javascript"> var btn1=document.getElementsByTagName('input')[0]; var btn2=document.getElementsByTagName('input')[1]; var p=document.getElementsByTagName('p')[0]; var timer; btn1.onclick=function(){ timer=setTimeout(function(){ p.innerHTML=''; },3000); } btn2.onclick=function(){ clearTimeout(timer); } </script> </body> </html>
Verification code countdown case:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <script> window.onload=function(){ var btn=document.getElementById("btn"); var times=10; var timer=null; btn.onclick=function(){ if(this.getAttribute("clicked")){return false;} var _this=this; timer=setInterval(function(){ times--; if(times<=0){ clearInterval(timer); _this.value="发送验证码"; //_this.disabled=false; _this.removeAttribute("clicked",false); times=10; }else{ _this.value=times+'秒后重试'; //_this.disabled=true; _this.setAttribute("clicked",true); } },1000) } } </script> </head> <body> <p class="box"> <input type="button" value="发送验证码" id="btn"> </p> </body> </html>
Flashing text:
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title>闪烁的文字</title> <style type="text/css"> p{ width:200px; height:200px; line-height:200px; border:2px solid gray; text-align:center; color:red; } </style> </head> <body> <h3 id="会闪烁的文字">会闪烁的文字</h3> <p id="text"> </p> <script type="text/javascript"> var text=document.getElementById('text'); var flag=0; setInterval(function(){ if(flag==0){ flag=1; text.innerHTML='☆☆☆今日特卖☆☆☆'; }else if(flag==1){ flag=0; text.innerHTML='★★★今日特卖★★★'; } },500); </script> </body> </html>
location.href returns the complete URL of the current page
location.hash Return to # after Step
console.log(location.href); console.log(location.hash); var btn=document.getElementById("btn"); btn.onclick=function(){ // 可以实现跳转 location.hash="#top"; } // 返回服务器名称和端口号 // 本地不行,要到服务器上 console.log(location.host); // 返回服务器名称 console.log(location.hostname); // 返回URL中的目录和文件名 console.log(location.pathname); // 返回URL中的查询字符串,以?开头 console.log(location.search);
screen object
setTimeout(function(){ // 会在历史记录中生成新纪录 location.href='index6.html'; window.location='index6.html'; // 不会在历史记录中生成新纪录 location.replace("index6.html"); },1000) document.getElementById("reload").onclick=function(){ // 有可能从缓存中加载 location.reload(); // 从服务器重新加载 location.reload(true); }
navigator object
var btn = document.getElementById("btn"); var btn2 = document.getElementById("btn2"); var btn3 = document.getElementById("btn3"); // 点击btn按钮时回到历史记录的上一步,后退 btn.onclick = function() { // 方法一 history.back(); // 方法二 history.go(-1); } // 点击btn2按钮时回到历史记录的下一步,前进 btn2.onclick = function() { // 方法一 history.forward(); // 方法二 history.go(1); } btn3.onclick = function() { // 前进n步 history.go(n); // 后退n步 history.go(-n); }
This article comes from the
js tutorial column, welcome to learn!
The above is the detailed content of Knowledge points on BOM operation in JS. For more information, please follow other related articles on the PHP Chinese website!

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.

I built a functional multi-tenant SaaS application (an EdTech app) with your everyday tech tool and you can do the same. First, what’s a multi-tenant SaaS application? Multi-tenant SaaS applications let you serve multiple customers from a sing

This article demonstrates frontend integration with a backend secured by Permit, building a functional EdTech SaaS application using Next.js. The frontend fetches user permissions to control UI visibility and ensures API requests adhere to role-base

JavaScript is the core language of modern web development and is widely used for its diversity and flexibility. 1) Front-end development: build dynamic web pages and single-page applications through DOM operations and modern frameworks (such as React, Vue.js, Angular). 2) Server-side development: Node.js uses a non-blocking I/O model to handle high concurrency and real-time applications. 3) Mobile and desktop application development: cross-platform development is realized through ReactNative and Electron to improve development efficiency.

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

Python is more suitable for data science and machine learning, while JavaScript is more suitable for front-end and full-stack development. 1. Python is known for its concise syntax and rich library ecosystem, and is suitable for data analysis and web development. 2. JavaScript is the core of front-end development. Node.js supports server-side programming and is suitable for full-stack development.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 Chinese version
Chinese version, very easy to use

WebStorm Mac version
Useful JavaScript development tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft