This site is mobile accessible. Press the "Tap Here" button to use a different font-size.


Smartphone icons created by Freepik - Flaticon

  • 15.3 JS Location

    The window.location object can be used to get the current page address (URL) and to redirect the browser to a new page.

    Window Location

    • The window.location object can be written without the window prefix.
    • Some examples:
      • window.location.href returns the href (URL) of the current page
      • window.location.hostname returns the domain name of the web host
      • window.location.pathname returns the path and filename of the current page
      • window.location.protocol returns the web protocol used (http: or https:)
      • window.location.assign() loads a new document

    Window Location Href

    The window.location.href property returns the URL of the current page

    Example 1: JS Location - Window Location Href

    Display the href (URL) of the current page:

    document.getElementById("demo").innerHTML =
    "Page location is " + window.location.href;

    Result is:

    Page location is https://a9758-2817847.cluster61.canvas-user-content.com/courses/9758~608416/files/9758~2817847/course%20files/js/js_window_location.asp.html?download=1&inline=1&sf_verifier=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJ1c2VyX2lkIjoiMTcwMDAwMDA0NTk5NDYxIiwicm9vdF9hY2NvdW50X2lkIjoiOTc1ODAwMDAwMDAwMDA5NzUiLCJvYXV0aF9ob3N0IjoiemZhcm53b3J0aC5pbnN0cnVjdHVyZS5jb20iLCJyZXR1cm5fdXJsIjpudWxsLCJmYWxsYmFja191cmwiOiJodHRwczovL3pmYXJud29ydGguaW5zdHJ1Y3R1cmUuY29tL2NvdXJzZXMvNjA4NDE2L2ZpbGVzLzI4MTc4NDcvcHJldmlldz9mYWxsYmFja190cz0xNjEwMDQyMzg3IiwiZXhwIjoxNjEwMDQyNjg3fQ.butNoUU7PnHjicQNIqsdZl4N6r4ySzhpPks3CzaVE25J4gqOsrUlKNpFT0QYIw5Wa3UtskTM84sodK6ciMvygQ

    Window Location Hostname

    The window.location.hostname property returns the name of the internet host (of the current page).

    Example 2: JS Location - Window Location Hostname

    Display the name of the host:

    document.getElementById("demo").innerHTML =
    "Page hostname is " + window.location.hostname;

    Result is:

    Page hostname is a9758-2817847.cluster61.canvas-user-content.com

    Window Location Pathname

    The window.location.pathname property returns the pathname of the current page.

    Example 3: JS Location - Window Location Pathname

    Display the path name of the current URL:

    document.getElementById("demo").innerHTML =
    "Page path is " + window.location.pathname;

    Result is:

    Page path is /courses/9758~608416/files/9758~2817847/course%20files/js/js_window_location.asp.html

    Window Location Protocol

    The window.location.protocol property returns the web protocol of the page.

    Example 4: JS Location - Window Location Protocol

    Display the web protocol:

    document.getElementById("demo").innerHTML =
    "Page protocol is " + window.location.protocol;

    Result is:

    Page protocol is https:

    Window Location Port

    The window.location.port property returns the number of the internet host port (of the current page).

    Example 5: JS Location - Window Location Port

    Display the port number:

    document.getElementById("demo").innerHTML =
    "Port number is " + window.location.port;

    Result is:

    Port number is

    Most browsers will not display default port numbers (80 for http and 443 for https)

    Window Location Assign

    The window.location.assign() method loads a new document.

    Example 6: JS Location - Window Location Assign

    Load a new document:

    <html>
    <head>
    <script>
    function newDoc() {
      window.location.assign("https://www.w3schools.com")
    }
    </script>
    </head>
    <body>
    
    <input type="button" value="Load new document" onclick="newDoc()">
    
    </body>
    </html>
    Navigate this module

    Eventually the navigation links, above, will be replaced by these << (previous) and >> (next) buttons below.



    JavaScript icons used in the buttons provided by ICONS8.COM. Smartphone icons created by Freepik - Flaticon