Javascript learning notes (14) Introduction to window object usage _ basic knowledge

Source: Internet
Author: User
Introduction to the use of window objects in javascript learning notes. For more information, see section 1. window Location.
The following figure shows the position of the browser window from the left and top of the screen.

The Code is as follows:


Var leftPos = (typeof window. screenLeft = "number ")? Window. screenLeft: window. screenX; // left position
Var topPos = (typeof window. screenTop = "number ")? Window. screenTop: window. screenY; // above position


2. browser size
The following figure shows the size of the browser page view.

The Code is as follows:


Var pageWidth = window. innerWidth,
PageHeight = window. innerHeight;

If (typeof pageWidth! = "Number "){
If (document. compatMode = "CSS1Compat "){
PageWidth = document.doc umentElement. clientWidth;
PageHeight = document.doc umentElement. clientHeight;
} Else {
PageWith = document. body. clientWdith;
PageHeight = document. body. clientHeight;
}
}


3. Open or pop-up window
Window. the open () method can take four parameters. Generally, you only need to specify the first parameter, and the first parameter is URL, the second parameter is _ self, _ parent, _ top, _ blank, or framework name.

The Code is as follows:


Window. open ("http://www.baidu.com ");
Window. open ("http://www.baidu.com", "_ blank ");
Window. open ("http://www.baidu.com", "topFrame", "height = 400, width = 400, top = 10, left = 10, resizable = yes ");
TopFrame. resizeTo (500,300); // adjust the window size
TopFrame. moveTo (100,100); // move the window position
TopFrame. close (); // close the new window. IE will report an error


4. location object
Location. href (URL) Load URL

The Code is as follows:


Location. href (URL) Load URL
Location. href ("http://www.baidu.com ");
Location. href = "http://www.baidu.com"; // same as above
Location. assign = "http://www.baidu.com"; // same as above
Window. loaction = "http://www.baidu.com"; // same as above
Location. replace ("http://www.baidu.com"); // same as above, but cannot roll back

Location. reload (); // reload (may load from the cache)
Location. reload (true); // reload (load from the server)

Location. search () returns the query string in the URL. What is the string? Start


5. Obtain query string Parameters

The Code is as follows:


Function getQueryStringArgs (){
Var qs = (location. search. length> 0) location. search. substring (1 ):"";
Var args = {};
Var items = qs. split ("&");
Var item = null, name = null, value = null;
For (var I = 0; I {
Item = itmes [I]. split ("= ");
Name = decodeURIComponent (item [0]);
Value = decodeURIComponent (item [1]);
Args [name] = value;
}
Return args;
}

// Suppose the query string parameter is? Q = javascript & num = 10
Var args = getQueryStringArgs ();
Alert (args ["q"]); // "javascript"
Alert (args ["num"]); // "10"


6. history Object

The Code is as follows:


History. go () page Jump
History. go (-1); // return a page
History. go (1); // Previous Page
History. go (2); // two previous pages
History. go ("baidu.com"); jump to the most recent baidu.com page

History. back (); // returns a page
History. forword (); // Previous Page


Checks whether the current page is the first page opened by the user.

The Code is as follows:


If (history. length = 0 ){
// If the first page is opened, perform some operations
}


7. Page Loading
Window. onload () is used to perform some operations after page loading.

The Code is as follows:


Window. onload = function (){
// Perform some operations
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.