Baptiste Dufour

Bookmarklets: JavaScript in your bookmarks

Bookmarklets are coded in JavaScript
Bookmarklets are nothing more than JavaScript saved as bookmarks.

Bookmarklets are JavaScript programs that can be stored in your browser’s bookmarks. Unlike regular bookmarks, they don’t lead to websites but instead execute code on the page you’re currently viewing. They have a wide range of applications, but they’re often used to automate tedious tasks on a webpage. In this way, they’re somewhat similar to browser extensions, but are generally intended for simpler uses. In this article, I’ll show you how to use bookmarklets and then how to easily create your own.

  1. Saving and Using Bookmarklets: How to Do It
    1. Installing a Bookmarklet You’ve Coded
  2. How to Program a Bookmarklet Yourself
    1. Using the Browser’s Developer Tools
    2. A Concrete Example of a JavaScript Program
    3. Turn Your Code into a Bookmark
  3. What Are Bookmarklets Used For in Practice?
    1. Examples of Bookmarklet Use

Saving and Using Bookmarklets: How to Do It

It’s not very complicated. Just take the link below and click on it.

HelloWorld!

There you go, you’ve just used your first bookmarklet. As with any regular link, you can save it to your bookmarks bar. Now, if you go to another site and click on it, you’ll get the exact same effect as before.

Installing a Bookmarklet You’ve Coded

For bookmarklets that you create yourself, here’s how you should proceed:

How to Program a Bookmarklet Yourself

Using the Browser’s Developer Tools

Ideally, you have some basic knowledge of JavaScript. Turning your code into a bookmarklet is very easy; what’s likely more challenging is creating the script itself. You’ll need to be familiar with manipulating the DOM and the window object.

Here’s how I suggest you proceed:

This way, you can refine your program at your own pace and save it as a bookmarklet when you’re fully satisfied with it.

A Concrete Example of a JavaScript Program

With the following example, I’ll try to show you some concepts that will be useful for most applications. Let’s say you want to create a small bookmarklet to quickly retrieve and display SEO information on a page:

The goal of the program is to fetch all these elements through basic DOM manipulations and display them in a new tab, which you’ll open using the window object.

For DOM manipulation, I recommend getting familiar with the querySelector and querySelectorAll methods of the document object. You’ll frequently need them to access elements on the page.

/* Retrieve title content */ 
let title = document.querySelector("title").innerHTML

/* Retrieve all meta tags */
let metas = document.querySelectorAll("meta")

/* Find the relevant meta values */
let metaDescription = ""
let metaRobots = ""

metas.forEach(function(meta){
    if(meta.name){
        switch(meta.name){
            case "description":
                metaDescription = meta.content
                break
            case "robots":
                metaRobots = meta.content
                break
        }
    }
})

/* Count all links */
let nLinks = document.querySelectorAll("a").length

/* Get all header tags */
let hns = document.querySelectorAll("h1,h2,h3,h4,h5,h6")

Often, the easiest way to display the data you’re interested in is to open a new tab using window.open("","_blank") and write directly in HTML with document.write(). In our example, it looks like this:

/* Open new tab*/
let tab = window.open("","_blank")

/* Write information in new tab */
tab.document.write("<p><b>Title:</b> "+ title +"</p>")
tab.document.write("<p><b>Meta description:</b> "+ metaDescription +"</p>")
tab.document.write("<p><b>Meta robots:</b> "+ metaRobots +"</p>")
tab.document.write("<p><b>Number of links:</b> "+ nLinks +"</p>")

/* Write each header */
hns.forEach(function(hn){
    tab.document.write("<" + hn.tagName + ">")
    tab.document.write(hn.tagName + ": " + hn.innerHTML)
    tab.document.write("</" + hn.tagName + ">")
})

Turn Your Code into a Bookmark

However, you can’t save your script as-is into your bookmarks:

Of course, you won’t do this manually—there are specialized online tools for this, such as Bookmarklet Maker.

What Are Bookmarklets Used For in Practice?

Examples of Bookmarklet Use

Bookmarklets can save you a lot of time on tedious tasks. For example, you can use them to:

As we’ve seen earlier, bookmarklets are handy in SEO for quickly displaying metadata. They also allow you to quickly access URLs by passing parameters. For instance, you can perform a quick site search with Google operators like site:, or get insights from PageSpeed Insights. There are plenty of SEO bookmarklets available online.

In any case, if you have more specific needs, you now have the skills to create your own custom bookmarklets!