The URL Object
Find out what is a URL object and how to use it
URL is a namespace used to host 2 static methods used to manipulate URLs using Blobs:
URL.createObjectURL()
URL.revokeObjectURL()
Given a blob, you generate a URL to it using the URL.createObjectURL()
function:
const myURL = URL.createObjectURL(aBlob)
Once you have the blob URL, you can destroy it from memory using:
URL.revokeObjectURL(myURL)
In addition to this, URL offers a very different functionality through its constructor, which can be used to create a URL. You can call it like this:
const currentUrl = new URL(window.location.href)
Now currentUrl
has a set of properties you can use to inspect the URL:
hash
the hash fragmenthost
the domain + porthostname
the domainhref
contains the entire URLorigin
scheme + domain + portpassword
pathname
port
protocol
search
searchParams
username
which are the usual parts of a URL.
You can alter any of those, except origin
and searchParams
which are read only, and generate a new URL string by calling the toString()
method, or by referencing the href
property.
→ I wrote 17 books to help you become a better developer:
- C Handbook
- Command Line Handbook
- CSS Handbook
- Express Handbook
- Git Cheat Sheet
- Go Handbook
- HTML Handbook
- JS Handbook
- Laravel Handbook
- Next.js Handbook
- Node.js Handbook
- PHP Handbook
- Python Handbook
- React Handbook
- SQL Handbook
- Svelte Handbook
- Swift Handbook
Also, JOIN MY CODING BOOTCAMP, an amazing cohort course that will be a huge step up in your coding career - covering React, Next.js - next edition February 2025