Javascript commands for the most common actions

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

EventFires when
abortResource loading was aborted
blurElement loses focus
changeElement value changed (commit)
clickElement was clicked
errorAn error occurred loading a resource
focusElement receives focus
loadResource finished loading
mouseoverCursor moved over the element
mouseoutCursor moved off the element
selectText was selected
submitA form is being submitted
unloadThe 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

OptionValuesDescription
locationyes|noDoes the location bar show?
menubaryes|noDoes the menubar show?
scrollbarsyes|noDo scrollbars show?
statusyes|noDoes the status bar show?
titlebaryes|noDoes the titlebar show?
toolbaryes|noDoes the toolbar show?
resizableyes|noCan the window be resized?
heightpixelsHeight of window
widthpixelsWidth 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.

Leave a Reply

Your email address will not be published. Required fields are marked *