Section 13: understanding BOM


Browser object model: browser object model refers to browser related js operations
Usually window xxxx

1. Get browser height

1. Both width and height include the width of the scroll bar
2. This is the width and height of the browser, not the page

console.log(  window.innerWidth) // Browser width 
console.log( window.innerHeight )// Browser height, not page height 

2. Get some browser related information: navigator

console.log( window.navigator);
//appName: Netscape 
//appVersion: version number
//Platform: 'Win32' system version

II Related to browser address

1. Content related to browser operation address

 //"address") opens a page
 document.getElementById("btn").onclick = function () {
// 2.window.close() closes the current page;
// Note that the closed page can only be closed if it is opened through code. The directly opened page cannot be closed
document.getElementById("btn2").onclick = function () {

III Related to viewer events

1.window.onload: wait until the page document and resources are loaded, not immediately

2.onresize: this event will be triggered when the browser is reset

var html =  document.getElementsByTagName("html")[0];
//    console.log(html); = window.innerWidth/10 + "px";  //Dynamically set html root font size
window.onresize = function () {
	console.log("The browser size has been reset");
    // Assuming that any width is 10rem, set the root font size dynamically  
var html =  document.getElementsByTagName("html")[0];
//    console.log(html); = window.innerWidth/10 + "px";  //Dynamically set html root font size

3.onscroll: it will be triggered when the page scrolls, and the browser must be able to scroll

window.onscroll = function () {
    console.log("The page scrolled");

4. The distance the page is rolled off when scrolling

window.onscroll = function () {
// Get the rolled height
console.log(document.documentElement.scrollTop );  // Documented declaration 
console.log(document.body.scrollTop) //No document declaration 

1, If there is a document declaration, we use document Documentelement to obtain;
2, If there is no document declaration, we use document Body to get
IE : 1. If there is no document declaration, both can be used
2. If there is a document declaration, document Documentelement works, document Body doesn't work
safari : window.pageYoffset to get

4 . Explorer pop-up window

1. Pop up alert;
2.prompt: an input box will pop up
3.confirm query box

// 1. Pop up alert;
// alert(123);
// 2.prompt: an input box will pop up. If you click OK, it is the content in the input box. If you click Cancel, you will get a null return
// var str = prompt("please enter the content");
// console.log(str);
// 3.confirm query box, return value: if you click OK, return true; if you click Cancel, return false
var res =  confirm("Are you sure?");

V timer

1. Countdown timer: executes a piece of code after a period of time. The unit is milliseconds

setTimeout(function() {
    console.log("Code that will be executed after 3 seconds of timing");
 }, 2000);

2... Interval timer: specify a period of time to cycle and execute a piece of code

// setInterval will always be executed in the background;
var num = 0;
       console.log("Execute the code here every 1 second");
       // Modify the title of the page
       document.title = num;  //Modify the title of the page
    }, 1000);

3. Stop timer: 1. The return value of timer is a number; 2. Stop timer: cleartimeout (return value of timer);

var timer = setTimeout(function(){
    console.log("Code executed");
}, 3000);
// console.log(timer);
var ele = document.getElementsByClassName("btn")[0];
ele.onclick = function () {
    clearTimeout(timer);  //Stop Timer  

var timer = setInterval(function(){
}, 2000);
ele.onclick = function () {
    // clearTimeout(timer);  // Stop Timer  
    clearInterval(timer);  //Stop interval timer

Note: clearInterval and clearTimeout can be used with each other

4. The countdown timer can replace the interval timer

setInterval(function () {
}, 2000);

// Simulate interval timer through countdown timer 
function fn() {
   setTimeout(function () {
        console.log("Code executed");
    }, 2000);

5. All timer codes must lag behind js codes

setTimeout(() => {
}, 0);
setTimeout(() => {

6. This problem in the timer: if it is not the arrow syntax in the timer, its this points to the window;

        var obj = {
            name: "Zhang San",
            fn: function () {
                // Save the external this value through a variable 
                var that = this;
                setTimeout(function () {
                    // console.log("2",;
                    // What about printing the name in obj??
                }, 2000);

Keywords: Javascript html5 html

Added by akkad on Thu, 16 Dec 2021 22:55:12 +0200