location object in JavaScript

Window Location is a URL object. It is mostly used to redirect or reload the pages.

Properties

Property Description
location.hrefFull URL string.
location.protocolProtocol Used in URL
location.hostHostname string
location.hostnameDomain part of the URL
location.portPort Number of the URL
location.pathnameString starting from / giving path from the URL
location.searchQueryString starting from ? from the URL
location.hashString followed by # in URL
Above demo will give value of all properties of a simple URL ( without any query string or hash etc ) . We can create one location object by adding all the above properties to the URL and check the output.

Application of location Properties

location.href to extract path from URL

location.hostname to extract hostname from URL

location.reload to Refreshing the current page

Location Methods

Methods Description
location.assign()Opens the provided URL in browser.

You can use Back Button to return to this page.
<input type=button value='JavaScirpt Home by assign' onClick=window.location.assign('site_map.php');>
location.reload()Reloads Current URL.
<input type=button value='Refresh this Page' onClick=window.location.reload();>
location.replace()Replace with provided URL and opens the page.

While moving to provided ( URL) page , the current page will not be saved in session history, so you can't use Back button of browser to return to the parent ( this ) page. So a link is provided in Demo page to return to this page.

This is the main difference between location.assign() and location.replace()
<input type=button value='Demo page by Replace' onClick=window.location.replace('location-demo.php');>
location.toString()Returns the full URL as string .

On Click location.toString() Method will return one alert Box with the URL String
<input type=button value='Demo toStringe() by Alert' onClick=alert(window.location.toString());>

Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com




    Post your comments , suggestion , error , requirements etc here .




    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer