JavaScript provides several built-in browser objects that let you work with the current page, browser history, environment, and small popup dialogs:
alert(), confirm(), and prompt().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.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.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.onLine – true if online, else false.navigator.cookieEnabled – true if cookies are enabled.alert() – simple message + OK button.confirm() – OK/Cancel, returns true or false.prompt() – asks for user input, returns string or null.Use the location object to inspect or change the current page URL. Changing location.href or using assign()/replace() will navigate to another page.
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.
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 dialogs block script execution until the user responds. They are useful for simple feedback or confirmation but should be used sparingly in modern UIs.
| 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 |
<script>
console.log(location.href);
console.log(location.search);
location.assign("https://example.com/contact");
</script>
<button onclick="history.back()">Go Back</button>
<button onclick="history.forward()">Go Forward</button>
<button onclick="history.go(-2)">Go 2 Pages Back</button>
<script>
console.log("Language: " + navigator.language);
console.log("Cookies Enabled: " + navigator.cookieEnabled);
console.log("Online Status: " + navigator.onLine);
</script>
<script>
alert("Welcome to our website!");
</script>
<script>
let result = confirm("Delete this file?");
if (result) {
alert("File deleted");
} else {
alert("Operation cancelled");
}
</script>
<script>
let city = prompt("Enter your city:");
if (city) {
alert("You live in " + city);
}
</script>
https://example.com/contact.history.go(-2) jumps two steps back in one click.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.confirm() before destructive actions, like deleting important data.prompt() before using it.navigator.cookieEnabled before relying on cookies for sessions or preferences.navigator.language only as a hint for localization, not as a hard rule.location.search to read query parameters and show dynamic content.location.assign() that takes the user to a different page.navigator.appVersion in the console or on the page.navigator.onLine and the online/offline events.location.search.http or https) using location.protocol.navigator.cookieEnabled.history.go(-1) or history.back().location.hostname.navigator.platform.location.reload().