ReasonJun

javascript : Web API (location) 본문

Frontend/Javasciprt

javascript : Web API (location)

ReasonJun 2023. 6. 9. 00:19
728x90

In web development, the location object in JavaScript provides information about the current URL and allows you to interact with it. It represents the URL of the document being displayed in the browser window and provides methods and properties for working with different aspects of the URL.

Here are the key aspects of the location object:

  1. Accessing the Location Object: The location object is accessible through the window.location property.
  2. URL Information: The location object provides various properties to access information about the current URL:
    • href: Returns the complete URL of the current page.
    • protocol: Returns the protocol (e.g., "http:", "https:") of the current URL.
    • host: Returns the hostname and port number of the current URL.
    • hostname: Returns the hostname (domain) of the current URL.
    • port: Returns the port number of the current URL.
    • pathname: Returns the path of the current URL.
    • search: Returns the query string part of the current URL (everything after the ?).
    • hash: Returns the fragment identifier part of the current URL (everything after the #).
    console.log(location.href);      // Complete URL
    console.log(location.protocol);  // Protocol (e.g., "http:", "https:")
    console.log(location.host);      // Hostname and port number
    console.log(location.hostname);  // Hostname (domain)
    console.log(location.port);      // Port number
    console.log(location.pathname);  // Path
    console.log(location.search);    // Query string
    console.log(location.hash);      // Fragment identifier
    
    1. Modifying the URL: The location object allows you to modify parts of the URL or navigate to a new URL using various methods:
    • assign(url): Loads a new URL, replacing the current document.
    • replace(url): Replaces the current URL with a new URL in the history stack, without creating a new entry.
    • reload(): Reloads the current page.
    • href: Assigning a new URL to the location.href property has the same effect as calling assign(url).
    • Modifying other properties like protocol, host, pathname, search, or hash updates the corresponding parts of the URL.
    location.assign('<https://www.example.com>');  // Load a new URL
    location.replace('<https://www.example.com>'); // Replace the current URL
    location.reload();                           // Reload the current page
    
    1. Redirecting: You can use the location object to redirect users to another page by setting the location.href property or calling the location.assign(url) method.
    location.href = '<https://www.example.com>';  // Redirect to a new URL
    location.assign('<https://www.example.com>'); // Redirect to a new URL
    
    1. Reloading the Page: The location.reload() method reloads the current page. By default, it reloads the page from the cache, but you can force a fresh reload by passing true as an argument.
    location.reload();         // Reload the current page from cache
    location.reload(true);     // Force a fresh reload, ignoring the cache
    
    The location object is commonly used for URL manipulation, navigation, and redirection within web applications. It provides an interface to retrieve and modify different parts of the URL, enabling dynamic behavior based on the current URL.
728x90

'Frontend > Javasciprt' 카테고리의 다른 글

javascript : regexp pattern  (0) 2023.06.09
javascript : Regular expression (test, match, replace)  (0) 2023.06.09
javascript : Web API (history)  (0) 2023.06.09
javascript : Web APIs (storage)  (0) 2023.06.09
javascript : Web APIs (cookie)  (0) 2023.06.09
Comments