A quick reference of the JavaScript commands that come up in everyday browser scripting — event handlers, navigation, dialogs, dates, and pop-up windows. Updated for how these APIs look today; the core objects haven’t changed much, but a few patterns here are now worth avoiding.
Navigation
Go to the previous page (the back button):
<input type="button" value="Back" onclick="history.go(-1);">
history.back() and history.forward() do what you’d expect; see MDN’s History API reference for the full interface (including the modern pushState/popstate machinery).
Most common event handlers
| Event | Fires when |
|---|---|
abort | Resource loading was aborted |
blur | Element loses focus |
change | Element value changed (commit) |
click | Element was clicked |
error | An error occurred loading a resource |
focus | Element receives focus |
load | Resource finished loading |
mouseover | Cursor moved over the element |
mouseout | Cursor moved off the element |
select | Text was selected |
submit | A form is being submitted |
unload | The document is being unloaded |
Note the modern naming: handlers are attached lowercase today (el.addEventListener('click', fn)); the on-prefixed capitalized HTML attributes above still work but are the legacy inline style. See the MDN event reference for the complete catalogue.
Writing to the page
document.write("hello World");
Works, but document.write is now flagged by linters and spec authors alike — it blocks parsing and misbehaves on async-loaded pages. Build DOM nodes or set textContent instead.
Alerts and dialogs
alert("I'm an alert");
confirm() and prompt() round out the trio. One pattern from the original post worth revisiting — navigating based on confirm():
<script>
function goThere() {
if (confirm("Do you really want to go to this page?")) {
window.location = "http://example.com/go";
} else {
window.location = "http://example.com/stay";
}
}
</script>
<a href="javascript:goThere()">New page</a>
The javascript: URL works, but the current best practice is a real href with a click handler that calls preventDefault() — better for accessibility, middle-click, and crawlers.
Date formatting
function todaysDate() {
var today = new Date();
var day = today.getDate();
var month = today.getMonth() + 1; // months are 0-based
var year = today.getFullYear();
return month + "/" + day + "/" + year;
}
That’s the classic manual approach and it still works. These days, prefer toLocaleDateString() (or Intl.DateTimeFormat) for locale-aware formatting — it replaces the manual month/day assembly entirely.
Pop-up windows
| Option | Values | Description |
|---|---|---|
location | yes|no | Does the location bar show? |
menubar | yes|no | Does the menubar show? |
scrollbars | yes|no | Do scrollbars show? |
status | yes|no | Does the status bar show? |
titlebar | yes|no | Does the titlebar show? |
toolbar | yes|no | Does the toolbar show? |
resizable | yes|no | Can the window be resized? |
height | pixels | Height of window |
width | pixels | Width of window |
Example:
window.open("win2.html", "Window2", "width=310,height=600,scrollbars=yes");
Pop-ups are far less common now — browsers block unrequested ones, and window.open from a user gesture still works but modal <dialog> elements or in-page overlays are usually the better tool.
The original version of this list referenced Matt Kruse’s JavaScript site as a good general resource; that site is no longer online, so MDN links above are the standing reference.