# Why does an old script throw errors in a modern browser?

Source: https://codelifter.com/forum/posts/why-does-an-old-script-throw-errors-in-a-modern-browser/
Site: CodeLifter Forum, category Script Shelf
Published: 2026-12-01. Updated: 2026-12-01. Replies: 0.

## Question by CodeLifter editors (staff), 2026-12-01

An old script usually throws errors in a modern browser for one of five reasons: it runs before its elements exist, needs a library that is not loaded, calls something the browser lacks, runs in strict mode or as a module, or is blocked by a security rule. The first console error tells you which.

## How do you read the error?

Open the developer tools, switch to the Console tab and reload the page. Fix the first red error first: one early failure often stops the rest of the script, so later errors may disappear once it is fixed.

Each error line gives three things:

1. **The type**, such as `TypeError`, `ReferenceError` or `SyntaxError`. Defined in one sentence each: a `TypeError` means a value was the wrong kind, often `null` or `undefined`; a `ReferenceError` means a name does not exist where the code looks for it; a `SyntaxError` means the code could not be parsed at all, so none of that script ran.
2. **The message**, which names the value or property involved.
3. **The file and line**, as a link. Chrome's [Console reference](https://developer.chrome.com/docs/devtools/console/reference) explains that you can expand an error to see its stack trace, "a history of function calls (frames) that led to the error", with the latest frame at the top.

Wording differs by browser. MDN's error reference pages list each engine's version, so search MDN for the message if it is unfamiliar.

## What do the common errors mean in an old script?

| Error (Chrome wording first) | Usual cause in an old script | Fix |
| --- | --- | --- |
| `TypeError: Cannot read properties of null` or `of undefined` | `getElementById()` found nothing: the script runs in the `<head>` before the element exists, or the id's case does not match | Load the script with `defer`; check the id; test for `null` before use |
| `ReferenceError: $ is not defined` | jQuery or another library is missing, or loads after the script that uses it | Load the library first, in order |
| `ReferenceError: x is not defined` | The script calls a function or object from a file that is no longer included, or a name only an old browser had | Find where it was defined; replace browser-only names with standard APIs |
| `TypeError: x is not a function` | The method does not exist on that object in this browser, or the name has a typo | Check MDN for the method; test for it before calling |
| `ReferenceError: x is not defined` on an assignment, only in strict code | A variable was never declared, and the file is now in strict mode or a module | Declare it with `const` or `let` |
| `SyntaxError` in a file that used to run | Strict mode or a module rejects `with` statements and old octal numbers such as `0644` | Rewrite those lines |

Sources for each row: MDN's pages on ["x" is (not) "y"](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Cant_access_property), ["x" is not defined](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Not_defined), ["x" is not a function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Not_a_function), [assignment to undeclared variable](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Undeclared_var) and [strict mode](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode).

## Why do these show up now, when the script used to work?

**The page around it changed.** Old snippets were pasted inline at the exact spot in the body where they were needed. Move one into a file in the `<head>` and `document.getElementById("menu")` runs before `<div id="menu">` has been parsed, so it returns `null`. MDN's [getElementById page](https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementById) also notes the id is case-sensitive: `"Main"` does not find `id="main"`. Adding `defer` fixes the first case; MDN's [script reference](https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/script) says deferred scripts run "after the document has been parsed".

**The script is now strict.** If you added `"use strict";`, or loaded the file with `type="module"`, sloppy habits become errors. MDN's strict mode page says it "makes it impossible to accidentally create global variables": in Firefox the message is `assignment to undeclared variable`, while Chrome reports it as `x is not defined`. The same page says strict mode "prohibits `with`" and makes 0-prefixed octal literals a syntax error. Modules are always strict, and their names are not global, so inline `onclick="..."` handlers that call them fail too; the [modernising thread](/forum/posts/how-do-you-modernise-an-old-cut-and-paste-javascript-script/) covers that switch.

**The script relied on old browser behaviour.** Some snippets read a global `event` inside handlers. MDN's [Window.event page](https://developer.mozilla.org/en-US/docs/Web/API/Window/event) marks it deprecated, says it "is not universally supported", and tells you to use the `Event` passed into the handler instead. `document.all` is also deprecated; MDN suggests `document.querySelectorAll("*")` where you need every element.

**Something else is blocking it.** A blocked script often produces no JavaScript error at all, only a console warning. `document.write()` calls are ignored from `async` and `defer` scripts, which the [document.write thread](/forum/posts/why-do-old-scripts-use-documentwrite-and-what-replaces-it/) covers. Popups from `window.open()` return `null` when blocked, and code that then uses the result throws a `TypeError`; see the [popup replacement thread](/forum/posts/how-do-you-replace-an-old-popup-window-script-with-a-dialog/). A Content Security Policy refuses inline scripts and `eval()`, which a follow-up on the [old tutorial scripts thread](/forum/posts/is-it-safe-to-paste-a-script-from-an-old-tutorial-site/) explains, and `http://` script links are blocked as mixed content on HTTPS pages.

## A repeatable way to fix it

1. Save the page and script locally and serve the folder, for example with `python3 -m http.server 8000 --bind 127.0.0.1`, so the browser treats it like a real site.
2. Reload with the Console open and click the first error's file link to jump to the line.
3. Set a breakpoint on that line in the Sources panel, reload, and inspect the value that is `null`, `undefined` or missing.
4. Fix that one cause, reload, and repeat until the console is clean.
5. Then read the warnings, not only the errors. The [thread on whether old scripts still work](/forum/posts/do-old-cut-and-paste-scripts-still-work-in-current-browsers/) has a table of patterns that fail silently.

![Five steps for fixing errors in an old script, from serving it locally to reading the console warnings](/forum/img/posts/repeatable-way-to-fix-old-script-console-errors.webp "The five-step fix loop for old script errors, as given in this article")

If the fix list is long, consider whether the script is still worth keeping. The [Script Shelf](https://codelifter.com/scripts/) shows which old CodeLifter scripts were rewritten, replaced by the platform, or retired.

## Short version

- Read the first console error: its type, message and line point to the cause.
- `null` errors usually mean the script ran before its element existed; load it with `defer`.
- `not defined` errors mean a missing library, a missing file, or a name from an old browser.
- Strict mode and modules turn undeclared variables, `with` and old octal numbers into errors.
- Blocked features often log warnings, not errors, so read those too.

---
Cite as: CodeLifter Forum, "Why does an old script throw errors in a modern browser?", https://codelifter.com/forum/posts/why-does-an-old-script-throw-errors-in-a-modern-browser/, accessed 2026-12-02.
