← Back to Chapters

JavaScript Location, History, Navigator & Popups

? JavaScript Location, History, Navigator & Popups

⚡ Quick Overview

JavaScript provides several built-in browser objects that let you work with the current page, browser history, environment, and small popup dialogs:

  • location – works with the current URL and redirects.
  • history – moves backward/forward through the session history.
  • navigator – gives information about the browser, OS, and capabilities.
  • popups – built-in dialogs: alert(), confirm(), and prompt().

? Key Concepts

  • Location Object
    • location.href – full URL (read/write).
    • location.hostname – domain name (e.g. example.com).
    • location.pathname – path after the domain (e.g. /docs/page1.html).
    • location.protocol – protocol (e.g. https:).
    • location.port – port number (e.g. 8080).
    • location.hash – fragment part (e.g. #top).
    • location.search – query string (e.g. ?id=1).
    • location.assign() – redirects and adds to history.
    • location.replace() – redirects and replaces current history entry.
    • location.reload() – reloads the current page.
  • History Object
    • history.length – number of URLs in the session history.
    • history.back() – go to previous URL.
    • history.forward() – go to next URL.
    • history.go(n) – move n steps forward or backward.
  • Navigator Object
    • navigator.appName – browser name (legacy).
    • navigator.appVersion – browser version info.
    • navigator.userAgent – user agent string.
    • navigator.platform – OS platform (e.g. Win32).
    • navigator.language – user language (e.g. en-US).
    • navigator.onLinetrue if online, else false.
    • navigator.cookieEnabledtrue if cookies are enabled.
  • Popup Boxes
    • alert() – simple message + OK button.
    • confirm() – OK/Cancel, returns true or false.
    • prompt() – asks for user input, returns string or null.

? Syntax & Theory

? Location Object

Use the location object to inspect or change the current page URL. Changing location.href or using assign()/replace() will navigate to another page.

? History Object

The history object controls navigation through the tab’s history list. It cannot read URLs directly (for security) but can move the user backward or forward.

? Navigator Object

The navigator object exposes environment information like browser version, language, platform, and online status. It’s handy for feature detection and basic customization, but should not be used for strict browser detection.

? Popup Boxes

Popup dialogs block script execution until the user responds. They are useful for simple feedback or confirmation but should be used sparingly in modern UIs.

? Quick Summary Table

Object Property / Method Description
location href Full URL of current page
location reload() Reloads the page
history back() Go to previous URL
navigator language Browser language
popup alert() Shows a message popup
popup prompt() Asks for user input

? Code Examples

? Location Example

? View Location Code
<script>
console.log(location.href);
console.log(location.search);
location.assign("https://example.com/contact");
</script>

? History Example

? View History Code
<button onclick="history.back()">Go Back</button>
<button onclick="history.forward()">Go Forward</button>
<button onclick="history.go(-2)">Go 2 Pages Back</button>

? Navigator Example

? View Navigator Code
<script>
console.log("Language: " + navigator.language);
console.log("Cookies Enabled: " + navigator.cookieEnabled);
console.log("Online Status: " + navigator.onLine);
</script>

? Popup Examples

? View Alert Example
<script>
alert("Welcome to our website!");
</script>
? View Confirm Example
<script>
let result = confirm("Delete this file?");
if (result) {
  alert("File deleted");
} else {
  alert("Operation cancelled");
}
</script>
? View Prompt Example
<script>
let city = prompt("Enter your city:");
if (city) {
  alert("You live in " + city);
}
</script>

? Live Output & Explanation

?️ What These Snippets Do

  • The location example logs the current page URL and query string to the console, then redirects the user to https://example.com/contact.
  • The history buttons move the user backward and forward in the browser’s history. history.go(-2) jumps two steps back in one click.
  • The navigator example logs the browser language, whether cookies are enabled, and the current online status.
  • The alert popup simply shows a welcome message with an OK button.
  • The confirm popup asks the user if they want to delete a file, then shows a follow-up alert based on their choice.
  • The prompt popup asks for the user’s city and displays it in another alert if a value is provided.

? Tips & Best Practices

  • location.hash is useful for simple single-page app navigation and scrolling to sections.
  • location.replace() is ideal for login/logout redirects where you don’t want the user to go back.
  • navigator.onLine can be used to show offline/online banners to the user.
  • alert() is often replaced by custom modal dialogs in modern UIs.
  • Use confirm() before destructive actions, like deleting important data.
  • Always validate and sanitize user input from prompt() before using it.
  • Check navigator.cookieEnabled before relying on cookies for sessions or preferences.
  • Use navigator.language only as a hint for localization, not as a hard rule.
  • Use location.search to read query parameters and show dynamic content.

? Try It Yourself

  • Create a redirect button using location.assign() that takes the user to a different page.
  • Show the user's browser version using navigator.appVersion in the console or on the page.
  • Build a confirm box that logs the user out only if they click OK.
  • Prompt the user to input their email and validate the format with a simple regex.
  • Display a different message when offline using navigator.onLine and the online/offline events.
  • Access and display the current page's query string using location.search.
  • Display the current protocol (http or https) using location.protocol.
  • Show an alert if cookies are disabled using navigator.cookieEnabled.
  • Create a "Back" button using history.go(-1) or history.back().
  • Display the hostname of the current page using location.hostname.
  • Detect and display the user's operating system using navigator.platform.
  • Add a button that reloads the page using location.reload().