A Brief Introduction to location usage in javascript

Source: Internet
Author: User
Tags function examples

Previously I wrote a piece of window. location. href is used to refresh another framework page. Now I have read the detailed usage of locaiton, which is somewhat improved. Now I have compiled it into js to facilitate reading and share it with my friends, the details are as follows:

1. Briefly introduce the location attribute, usage, and related examples:
Location
Contains information about the current URL.

Description
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 has the following format:

Protocol // host: Port/path name # hash ID? Search criteria include:

Http://skylaugh.cnblogs.com/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
Hash: Specifies an anchor name in the URL.
Host: Specifies the host and domain name, or IP address, of a network host.
Hostname: Specifies the host: port portion of the URL.
Href: Specifies the entire URL.
Pathname: Specifies the URL-path portion of the URL.
Port: Specifies the communications port that the server uses.
Protocol: Specifies the beginning of the URL, including the colon.
Search: Specifies a query.

Method Overview
Reload Forces a reload of the window's current document.
Replace Loads the specified URL over the current history entry.


Main function examples, similar to other examples:
Hash:

NewWindow. location. href = http://skylaugh.cnblogs.com
NewWindow. location. hash = #59831


Host
A string specifying the server name, subdomain, and domain name.
NewWindow. location. href = http://skylaugh.cnblogs.com
NewWindow. location. host = skylaugh.cnblogs.com


Href
A string specifying the entire URL.

Window. location. href = "http://home.netscape.com /"


Pathname
A string specifying the URL-path portion of the URL.

Search
A string beginning with a question mark that specifies any query information in the URL.

NewWindow. location. href = http://skylaugh.cnblogs.com
NewWindow. location. search =? Newsid= 111

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 ";
}
</Script>

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.