← All posts

Tutorials

How to Run JavaScript on Any Web Page — Beginner Guide

Sometimes a website is almost right — you just wish it did one extra thing. The fix is often a few lines of JavaScript. This beginner guide shows you how to run JavaScript on any web page, from a quick one-off in the console to a rule that runs automatically every time you visit, with real examples along the way.

A quick safety note before anything else

JavaScript you run on a page has the same powers the page itself has — it can read content, click things and send requests. So the rule is simple and non-negotiable: only run code you understand. Never paste a script from a stranger, a comment, or a "do this trick" video into a console — that is exactly how account-theft scams work. Every example below is short enough to read and understand fully.

Method 1 — the DevTools console (one-off)

Every browser ships a JavaScript console. Press F12 and open the Console tab. Type an expression, press Enter, and it runs immediately against the current page:

// Count every link on the page
document.querySelectorAll('a').length

The console is perfect for experiments and quick checks. Its limit: nothing persists. Reload the page and your code is gone — you would have to paste it again every single time.

Method 2 — bookmarklets (saved, but manual)

A bookmarklet is a browser bookmark whose URL is JavaScript instead of an address. Click the bookmark and the code runs on the current page. It looks like this:

javascript:(function(){
  document.body.style.fontFamily = 'Georgia, serif';
})();

Bookmarklets are genuinely useful and they persist between sessions. But they have real limits:

Method 3 — a persistent rule (automatic)

To have JavaScript run automatically every time you open a matching page, you need an extension that injects it for you. JustZix does this with rules: a URL pattern plus the code to run. It is free, works in Chrome (and Edge, Brave, Opera, Vivaldi), and needs no account.

Walkthrough — a rule that runs your JS

  1. Install JustZix from the download page.
  2. Open the page you want to enhance.
  3. Click the JustZix icon and choose New rule.
  4. Set a URL pattern, e.g. https://example.com/*.
  5. Open the JS pane and paste your script.
  6. Save. The script now runs on every matching page load.

While you are building the script, the in-tab JS Console REPL lets you test expressions against the live page, and the six-tab Output Console shows logs, network activity and DataLayer events — so you do not need DevTools open at the same time. More on the features page.

Example 1 — clean up a noisy page

Remove sticky elements that follow you down the page:

// Remove sticky headers and floating widgets
document.querySelectorAll('*').forEach(el => {
  const pos = getComputedStyle(el).position;
  if (pos === 'fixed' || pos === 'sticky') {
    el.style.position = 'static';
  }
});

Example 2 — auto-click a button

Some sites hide content behind a "Show more" button. Click it for yourself automatically:

// Click the first "Show more" button on load
const btn = [...document.querySelectorAll('button')]
  .find(b => /show more|read more/i.test(b.textContent));
if (btn) btn.click();

Example 3 — wait for content that loads late

Modern sites add content after the initial load. A MutationObserver lets your code react when the element you want finally appears:

// Run once the target element shows up
const observer = new MutationObserver(() => {
  const target = document.querySelector('.comments-section');
  if (target) {
    target.scrollIntoView();
    observer.disconnect();
  }
});
observer.observe(document.body, { childList: true, subtree: true });

This pattern is the difference between a script that "sometimes works" and one that works reliably — most timing bugs in page scripts come from running before the element exists.

Choosing the right method

MethodPersists?Automatic?Best for
DevTools consoleNoNoOne-off experiments
BookmarkletYesNo (click)Occasional manual actions
JustZix ruleYesYesChanges you want every visit

Debugging when something does not work

See also

Want your JavaScript to run automatically, every visit, without pasting it each time? JustZix is free — install it from the download page and turn your snippet into a rule.

Rate this post

No ratings yet — be the first.

Try it yourself

Install JustZix and paste any snippet from this article. Two minutes from zero to a working rule across all your devices.

Get JustZix

Features · How it works · Examples · Use cases