JavaScript Location Object Usage detailed

Source: Internet
Author: User
Tags hash readable

Location objects
The Location object contains information about the current URL, a part of the Window object that can be accessed through the Window.location property

Location Object Properties

The href property is a readable, writable string that sets or returns the full URL of the currently displayed document, so we can read and display the contents of the new URL by setting a new URL for the property

Syntax: Location.href = URL;

Example: location.href = ' http://www.111cn.net ';

A hash is a readable, writable string that is the anchor part of a URL (the part that begins with the # number).
Grammar: Location.hash = anchorname;
Example: Location.hash = ' myanchor '; <a name= "Myanchor" > Jump to Here </a>//page jump
You can use this property and anchor points to implement jumps within the page

Host returns the name and port number of the current URL
Grammar: Location.host;
Example: document.write (Location.host); URL address is: http://192.168.1.101/test/test.php output 192.168.1.101

Hostname returns the host name of the current URL
Grammar: Location.hostname;
Example: document.write (Location.hostname); URL address is: http://192.168.1.101/test/test.php output 192.168.1.101

Pathname is a readable, writable string that sets or returns the path portion of the current URL
Syntax: location.pathname = path;
Example: document.write (Location.pathname); URL address is: http://192.168.1.101/test/test.php output/test/test.php
Location.pathname = ' test/test3.php '; Jump to test3.php page

Port is a readable and writable string that can set or return the ports portion of the current URL
Grammar: Location.port = portnumber;
This did not do experiments, you can experiment, talk about how it!

Protocol is a readable and writable string that sets or returns the protocol for the current URL
Syntax: location.protocol = path;
Example: document.write (Location.protocol); The URL address is: http://192.168.1.101/test/test.php output http:

Search is a readable and writable string that sets or returns the query portion of the current URL (question mark?). After the section)
Grammar: Location.search = Path_from_questionmark;
Example: document.write (Location.search); The URL address is: http://192.168.1.101/test/test.php?key=test output? key=test
Location.search = ' key=test '; Look at the URL address has become: http://192.168.1.101/test/test.php?key=test

Location Object method
Assign () loads a new document
Syntax: Location.assign (URL);
Example: Location.assign (' http://www.111cn.net ');

Reload () is used to reload the current document
Syntax: Location.reload ();

Replace () replaces the current document with a new document, and the Replace () method does not generate a new record in the History object, and when this method is used, the new URL overwrites the current record in the History object
Grammar: Location.replace (Newurl);
Example: Location.replace (' http://www.111cn.net ');

Related Article

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.