Location address object in Javascript

Source: Internet
Author: User

The location object describes the addresses opened by a window object. To represent the address of the current window, you only need to use "location". To represent the address of a window, use "<window Object>. Location ". Previously, I wrote window. Location. href to refresh another framework page. I have read the detailed usage of locaiton, which is somewhat improved, as shown below:

Note: The two addresses of different protocols or hosts cannot reference the location object of each other. This is a security requirement. For example, the current window opens a page under "www.a.com", and another window (Object Name: bwindow) opens a webpage of "www. B .com. If "bwindow. Location" is used in the current window, an error occurs: "No permission ". This error cannot be handled.Program(Event handler, refer to onerror events) to receive and process events.

1. Briefly introduce the location attribute, usage, and related examples:
Location
Contains information about the current URL. The location object describes the complete URL associated with a given window object. Each attribute of the location object describes the different features of the URL.
Generally, a URL will have the following format: Protocol // host: Port/path name # hash ID? SEARCH CRITERIA
Example: http://www.ijavascript.cn/jiaocheng/index.html#topic1? X = 7 & Y = 2:

"Protocol" is the starting part of the URL until it contains the first colon.

"Host" describes the host and domain name, or the IP address of a network host.

"Port" describes the communication port used by the server for communication.

The path name describes the URL path information.

The "hash ID" describes the anchor name in the URL, including the hash mask (#). This attribute applies only to HTTP URLs.

"Search criteria" describes any query information in the URL, including question marks. This attribute applies only to HTTP URLs. The "Search Condition" string contains the pairing of variables and values. Each pair is connected by.

Attribute Overview

Protocol, which can be set to 'HTTP: ', 'https:', or 'file.

Hostname: The Host Name of the returned address. For example, for an address of "http://www.microsoft.com/china/#, location.hostname = 'www .microsoft.com '.

Port: the port number of the returned address. Generally, the http port number is '80 '.

Host returns the host name and port number, for example, 'www .a.com: 8080 '.

Pathname: return the path name, for example, http://www.a.com/ B /c.html#,location.pathname = 'B/c.html '.

Hash returns "#" and subsequent content, such as "http://www.a.com/ B /c.html?chapter4=, location.hash = '# Chapter4'; if" # "is not in the address, an empty string is returned.

Search returns "?" And later content, such as "http://www.a.com/ B /c.asp? Selection = 3 & jumpto = 4 ", location. Search = '? Selection = 3 & jumpto = 4'; if the address does not contain "?", Returns an empty string.

Href returns all the preceding content, that is, the entire address. Display it in the address bar of the browser and return it. If you want to open an address for a window object, you can use "location. href = '... '", you can also directly use" location = '....

Method Overview

Reload () is equivalent to pressing the "refresh" (IE) or "reload" (Netscape) key on the browser.

Replace () opens a URL and replaces the current address of the historical object. After opening a URL using this method, pressing the "back" Key of the browser will not be able to return to the previous page.

2. Redirect the location page to JS as follows:
// Simple jump
Function gotopage (URL ){
// Eg. var url = "newsview.html? Catalogid = "+ catalogid +" & pageid = "+ pageid;
Window. Location = URL;
}

// Upgrade the location usage to pass parameters for a single page
Function goto_catalog (ICAT ){
If (ICAT <= 0 ){
Top. Location = "../index. aspx"; // top
} Else {
Window. Location = ".../newscat. aspx? Catid = "+ ICAT;
}
}

// Jump to the page of the specified framework. Both methods are available.
Function goto_iframe (URL ){
Parent. Mainframe. Location = ".../index. aspx ";//
// Parent.doc ument. getelementbyid ("mainframe"). src = ".../index. aspx"; // use Dom to change page // At the same time, I added Dom writing.
}

// Jump to the page for the specified framework, because parent. iframename. location = ".. /index. aspx "; the method cannot be implemented, mainly" parent. iframename in iframename is set as a node by default in JS, but the passed parameters cannot be converted. Therefore, Dom is used to implement the frame jump page for passing two parameters, I hope that this old man will be enlightened!
Function goto_iframe (iframename, URL ){
Parent.doc ument. getelementbyid (iframename). src = ".../index. aspx"; // use Dom to change page by iframename
//}

// Return to the home page
Function Gohome (){
Top. Location = "/index. aspx ";
}

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.