An in-depth analysis of JavaScript advanced BOM technology
This article brings you relevant knowledge about BOM in JavaScript. BOM is composed of a series of related objects, and each object provides many methods and properties. I hope it will be helpful to everyone.
Directory Overview
1. BOM Overview
- BOM = Browser Object Model Browser Object Model
- It provides objects that interact with browser windows independently of content. Its core object is window
- BOM It is composed of a series of related objects, and each object provides many methods and attributes
- BOM lacks standards. The standardization organization for JavaScript syntax is ECMA, the standardization organization for DOM is W3C, and BOM was originally the Netscape browser Part of the standard
DOM | BOM |
---|---|
Document Object Model | Browser Object Model |
document as a object | Treat thebrowser as a object |
document## The top-level object of #BOM is | window|
BOM is to learn some objects for browser window interaction. | |
BOM is defined by browser manufacturers on their respective browsers, and has poor compatibility |
组成 | 说明 |
---|---|
protocol | 通信协议 常用的http,ftp,maito等 |
host | 主机(域名) www.itheima.com |
port | 端口号,可选 |
path | 路径 由零或多个'/' 符号隔开的字符串 |
query | 参数 以键值对的形式,通过& 符号分隔开来 |
fragment | 片段 # 后面内容 常见于链接 锚点 |
5.2、location对象属性
location对象属性 | 返回值 |
---|---|
location.href | 获取或者设置整个URL |
location.host | 返回主机(域名)www.baidu.com |
location.port | 返回端口号,如果未写返回空字符串 |
location.pathname | 返回路径 |
location.search | 返回参数 |
location.hash | 返回片段 #后面内容常见于链接 锚点 |
重点记住:href
和search
需求:5s之后跳转页面
<button>点击</button> <p></p> <script> var btn = document.querySelector(&#39;button&#39;); var p = document.querySelector(&#39;p&#39;); var timer = 5; setInterval(function() { if (timer == 0) { location.href = &#39;http://www.itcast.cn&#39;; } else { p.innerHTML = &#39;您将在&#39; + timer + &#39;秒钟之后跳转到首页&#39;; timer--; } }, 1000); </script>
5.3、location对象方法
location对象方法 | 返回值 |
---|---|
location.assign() | 跟href一样,可以跳转页面(也称为重定向页面) |
location.replace() | 替换当前页面,因为不记录历史,所以不能后退页面 |
location.reload() | 重新加载页面,相当于刷新按钮或者 f5 ,如果参数为true 强制刷新 ctrl+f5 |
<button>点击</button> <script> var btn = document.querySelector(&#39;button&#39;); btn.addEventListener(&#39;click&#39;, function() { // 记录浏览历史,所以可以实现后退功能 // location.assign(&#39;http://www.itcast.cn&#39;); // 不记录浏览历史,所以不可以实现后退功能 // location.replace(&#39;http://www.itcast.cn&#39;); location.reload(true); }) </script>
5.4、获取URL参数
我们简单写一个登录框,点击登录跳转到 index.html
接下来我们写 index.html
<p></p> <script> console.log(location.search); // ?uname=andy // 1.先去掉? substr(&#39;起始的位置&#39;,截取几个字符); var params = location.search.substr(1); // uname=andy console.log(params); // 2. 利用=把字符串分割为数组 split(&#39;=&#39;); var arr = params.split(&#39;=&#39;); console.log(arr); // ["uname", "ANDY"] var p = document.querySelector(&#39;p&#39;); // 3.把数据写入p中 p.innerHTML = arr[1] + &#39;欢迎您&#39;; </script>
这样我们就能获取到路径上的URL参数
6、navigator对象
- navigator 对象包含有关浏览器的信息,它有很多属性
- 我们常用的是
userAgent
,该属性可以返回由客户机发送服务器的user-agent
头部的值
下面前端代码可以判断用户是用哪个终端打开页面的,如果是用 PC 打开的,我们就跳转到 PC 端的页面,如果是用手机打开的,就跳转到手机端页面
if((navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i))) { window.location.href = ""; //手机 } else { window.location.href = ""; //电脑 }
7、history对象
- window 对象给我们提供了一个 history 对象,与浏览器历史记录进行交互
- 该对象包含用户(在浏览器窗口中)访问过的 URL。
history对象方法 | 作用 |
---|---|
back() | 可以后退功能 |
forward() | 前进功能 |
go(参数) | 前进后退功能,参数如果是 1 前进1个页面 如果是 -1 后退1个页面 |
<a>点击我去往列表页</a> <button>前进</button> <script> var btn = document.querySelector(&#39;button&#39;); btn.addEventListener(&#39;click&#39;, function() { // history.forward(); history.go(1); }) </script>
相关推荐:javascript学习教程
The above is the detailed content of An in-depth analysis of JavaScript advanced BOM technology. For more information, please follow other related articles on the PHP Chinese website!

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

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.


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

SublimeText3 Chinese version
Chinese version, very easy to use

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver CS6
Visual web development tools

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),

Zend Studio 13.0.1
Powerful PHP integrated development environment