js BOM foundation

window object
ECMAScript is the core javascript, but if you want to use javascript in the web, the BOM (Browser Object Model) is the real core. BOM provides a number of objects, functions for accessing browser, these functions are independent of any web content.
window object: BOM is the core object window, it represents an instance of the browser. In the browser, window object has a dual role, both as an interface to access a browser window through javascript, but also a Global ECMAScript objects specified.
    Therefore, all the variables declared in global scope, functions and methods will become property of the window object.
 
<Script type = "text / JavaScript">
    var Age = 26 is; // global variables and functions defined herein is automatically classified in the name of the object window
    function sayAge () {
        the console.log (this.age);
    }
    Console .log (window.age); // 26 is
    sayAge (); // 26 is equivalent window.sayAge ()
    window.sayAge (); // 26 is
    // global variables and the only difference is defined directly on the window object attributes: global variables can not be deleted by the delete operator, directly on the window defined object properties can
    window.color = 'Red';
    delete window.age;
    delete window.color;
    the console.log (window.age); // 26 is
    the console.log (window.color); // undefined
    </ Script> 12345678910111213141516
 
<Script of the type = "text / JavaScript">
        / *
        Also note: An attempt to access an undeclared variable will throw an error, but by querying the window object, you can know a possible undeclared variable exists
         * /
        // This throws an error, because oldValue undefined
        var newValue = oldValue;
        // this will not throw an error, because it is a property query
        var newValue = window.oldValue;
    </ Script> 123456789
 
Relations and window frame
If the page contains frames, each frame has its own window object, and is stored in the frames collection. In the set of frames, by the index value (from zero, left to right, top to bottom) or the frame name to access the corresponding window object. Each window object has a name attribute, which contains the name of the frame.
Can be referenced by the frame above window.frames [0] or window.frames [ "topFrame"]. However, it is best to use top, rather than a window to refer to these frameworks. Because, top object is always the highest point framework (outermost) layer, which is the browser window. Use it to ensure proper access to another frame in a frame. Because for any code written in a frame, the point where the window object is a specific instance of that framework, rather than the top of the frame.
And another opposite top window object is parent. parent object is always pointing to the current direct the upper frame of the frame.
Finally, a framework for object is self, it always points to the window. self and window objects are used interchangeably.
In the case of the framework, the browser will be multiple Global objects. Global variables defined in each frame will automatically become the object properties of the window frame. Since each window object contains native type constructors, each frame has its own set of constructors, constructors one correspondence, but not equal.
 
The location object
// object properties and methods
    // location objects
    //console.log(window.location);
// // in the address bar and the back of the # content
// console.log (window.location.hash);
// // host name and port number
// console.log (window.location.host);
// / / hostname
// console.log (window.location.hostname);
path // // relative path to the file ---
// console.log (window.location.pathname);
// // port number
// console .log (window.location.port);
// // protocol
// console.log (window.location.protocol);
content // // search
// console.log (window.location.search);
the onload = function () {
   document.getElementById ( "BTN") the onclick = function () {.
        // set the address of the page from
       //location.href="http://www.jd.com";// properties - ----------------> must remember
       //location.assign("http://www.jd.com");// method
        //location.reload();// reload - refresh
        //location.replace("http://www. jd.com "); // no history, can not return
   };
};
 
history objects
history object holds the history of the Internet users, from the moment the window was open date. Because history is a property of the window object, so each browser window, each tab and each frame has its own history object associated with a particular window object. Consider, developers in security is impossible to know the URL the user visited, however, with the list of pages visited by the user, can also be realized back and forward without knowing the actual URL situation.
Use go () method can be any jump in the history of a user, you can also back forward. The method takes one parameter: the integer value of the number of the page to jump forward or backward. A negative number indicates a jump backward (similar to click the browser's back button), a positive number represents a jump forward (forward button browser-like).

Guess you like

Origin www.cnblogs.com/chuliuxiang/p/10972815.html