The location of Windows in JavaScript programming and the detailed knowledge of history objects _ basics

Source: Internet
Author: User

Window Location

    • The Window.location object is used to get the address (URL) of the current page and to redirect the browser to a new page.
    • The Window.location object does not use the prefix of window when writing. Some examples:
    • Some examples:
    • Location.hostname returns the domain name of the web host
    • Location.pathname returns the path and filename of the current page
    • Location.port return to the web host's port (80 or 443)
    • Location.protocol returns the Web protocol used (http://or https://)

Window Location Href

The Location.href property returns the URL of the current page.
Instance
Returns the entire URL (of the current page):

<script>

document.write (location.href);

</script>



Window Location Pathname
The Location.pathname property returns the path name of the URL.
Instance
Returns the path name of the current URL:

<script>

document.write (location.pathname);

</script>

The above code output is:

/js/js-window-location.html


Window Location Assign
the Location.assign () method loads the new document.
Instance
To load a new document:

 
 


Window History
the Window.history object does not use the prefix of window when writing.
To protect user privacy, there are restrictions on how JavaScript accesses the object.
Some methods:

    • History.back ()-Same as clicking the Back button in the browser
    • History.forward ()-Same as clicking the button in the browser forward

Window History Back

The History.back () method loads the previous URL in the History list.
This is the same as clicking the Back button in the browser:
Instance
To create a Back button on a page:

 
 


Window History Forward
The history Forward () method loads the next URL in the history list.
This is the same as clicking the Forward button in the browser:
Instance
Create a Forward button on the page:

 
 

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.