URL
interface
interface URLThe URL interface represents an object providing static methods used for creating, parsing, and manipulating URLs in Ende.
Use the URL API for safely parsing, constructing, normalizing, and encoding URLs. This is the preferred way to work with URLs in Ende rather than manual string manipulation which can lead to errors and security issues.
See also
variable
var URL: { … }The URL interface represents an object providing static methods used for creating, parsing, and manipulating URLs.
See also
Constructors
constructor
new URL(url: string | URL, base?: string | URL): URLCreates a new URL object by parsing the specified URL string with an optional base URL. Throws a TypeError If the URL is invalid or if a relative URL is provided without a base.
Use this to parse and validate URLs safely. Use this instead of string manipulation to ensure correct URL handling, proper encoding, and protection against security issues like path traversal attacks.
Example
// Creating a URL from an absolute URL string
const url1 = new URL('https://example.org/foo');
console.log(url1.href); // Logs "https://example.org/foo"
// Creating a URL from a relative URL string with a base URL
const url2 = new URL('/bar', 'https://example.org');
console.log(url2.href); // Logs "https://example.org/bar"
// Joining path segments safely (prevents path traversal)
const baseUrl = 'https://api.example.com/v1';
const userInput = '../secrets'; // Potentially malicious input
const safeUrl = new URL(userInput, baseUrl);
console.log(safeUrl.href); // Correctly resolves to "https://api.example.com/secrets"
// Constructing URLs with proper encoding
const search = 'query with spaces';
const url3 = new URL('https://example.org/search');
url3.searchParams.set('q', search); // Automatically handles URL encoding
console.log(url3.href); // "https://example.org/search?q=query+with+spaces"
See also
Properties
hash
hash: stringThe hash property of the URL interface is a string that starts with a # and is followed by the fragment identifier of the URL.
It returns an empty string if the URL does not contain a fragment identifier.
Example
const myURL = new URL('https://example.org/foo#bar');
console.log(myURL.hash); // Logs "#bar"
const myOtherURL = new URL('https://example.org');
console.log(myOtherURL.hash); // Logs ""
See also
host
host: stringThe host property of the URL interface is a string that includes the URL.hostname and the URL.port if one is specified in the URL includes by including a : followed by the port number.
Example
const myURL = new URL('https://example.org/foo');
console.log(myURL.host); // Logs "example.org"
const myOtherURL = new URL('https://example.org:8080/foo');
console.log(myOtherURL.host); // Logs "example.org:8080"
See also
hostname
hostname: stringThe hostname property of the URL interface is a string that represents the fully qualified domain name of the URL.
Example
const myURL = new URL('https://foo.example.org/bar');
console.log(myURL.hostname); // Logs "foo.example.org"
See also
href
href: stringThe href property of the URL interface is a string that represents the complete URL.
Example
const myURL = new URL('https://foo.example.org/bar?baz=qux#quux');
console.log(myURL.href); // Logs "https://foo.example.org/bar?baz=qux#quux"
See also
origin
readonly origin: stringThe origin property of the URL interface is a string that represents the origin of the URL, that is the URL.protocol, URL.host, and URL.port.
Example
const myURL = new URL('https://foo.example.org/bar');
console.log(myURL.origin); // Logs "https://foo.example.org"
const myOtherURL = new URL('https://example.org:8080/foo');
console.log(myOtherURL.origin); // Logs "https://example.org:8080"
See also
password
password: stringThe password property of the URL interface is a string that represents the password specified in the URL.
Example
const myURL = new URL('https://someone:[email protected]/baz');
console.log(myURL.password); // Logs "somepassword"
See also
pathname
pathname: stringThe pathname property of the URL interface is a string that represents the path of the URL.
Example
const myURL = new URL('https://example.org/foo/bar');
console.log(myURL.pathname); // Logs "/foo/bar"
const myOtherURL = new URL('https://example.org');
console.log(myOtherURL.pathname); // Logs "/"
See also
port
port: stringThe port property of the URL interface is a string that represents the port of the URL if an explicit port has been specified in the URL.
Example
const myURL = new URL('https://example.org:8080/foo');
console.log(myURL.port); // Logs "8080"
const myOtherURL = new URL('https://example.org/foo');
console.log(myOtherURL.port); // Logs ""
See also
protocol
protocol: stringThe protocol property of the URL interface is a string that represents the protocol scheme of the URL and includes a trailing :.
Example
const myURL = new URL('https://example.org/foo');
console.log(myURL.protocol); // Logs "https:"
See also
search
search: stringThe search property of the URL interface is a string that represents the search string, or the query string, of the URL.
This includes the ? character and the but excludes identifiers within the represented resource such as the URL.hash. More granular control can be found using URL.searchParams property.
Example
const myURL = new URL('https://example.org/foo?bar=baz');
console.log(myURL.search); // Logs "?bar=baz"
const myOtherURL = new URL('https://example.org/foo?bar=baz#quux');
console.log(myOtherURL.search); // Logs "?bar=baz"
See also
searchParams
readonly searchParams: URLSearchParamsThe searchParams property of the URL interface provides a direct interface to
query parameters through a URLSearchParams object.
This property offers a convenient way to:
- Parse URL query parameters
- Manipulate query strings
- Add, modify, or delete URL parameters
- Work with form data in a URL-encoded format
- Handle query string encoding/decoding automatically
Example
// Parse and access query parameters from a URL
const myURL = new URL('https://example.org/search?term=ende&page=2&sort=desc');
const params = myURL.searchParams;
console.log(params.get('term')); // Logs "ende"
console.log(params.get('page')); // Logs "2"
// Check if a parameter exists
console.log(params.has('sort')); // Logs true
// Add or modify parameters (automatically updates the URL)
params.append('filter', 'recent');
params.set('page', '3');
console.log(myURL.href); // URL is updated with new parameters
// Remove a parameter
params.delete('sort');
// Iterate over all parameters
for (const [key, value] of params) {
console.log(`${key}: ${value}`);
}
See also
username
username: stringThe username property of the URL interface is a string that represents the username of the URL.
Example
const myURL = new URL('https://someone:[email protected]/baz');
console.log(myURL.username); // Logs "someone"
See also
prototype
readonly prototype: URLMethods
toString
toString(): stringThe toString() method of the URL interface returns a string containing the complete URL.
Example
const myURL = new URL('https://foo.example.org/bar');
console.log(myURL.toString()); // Logs "https://foo.example.org/bar"
See also
toJSON
toJSON(): stringThe toJSON() method of the URL interface returns a JSON representation of the URL.
Example
const myURL = new URL('https://example.org/foo');
console.log(myURL.toJSON()); // Logs "https://example.org/foo"
See also
parse
parse(url: string | URL, base?: string | URL): URL | nullParses a URL string or URL object and returns a URL object.
Example
const myURL = URL.parse('https://example.org');
console.log(myURL.href); // Logs "https://example.org/"
console.log(myURL.hostname); // Logs "example.org"
console.log(myURL.pathname); // Logs "/"
console.log(myURL.protocol); // Logs "https:"
const baseURL = new URL('https://example.org');
const myNewURL = URL.parse('/foo', baseURL);
console.log(myNewURL.href); // Logs "https://example.org/foo"
console.log(myNewURL.hostname); // Logs "example.org"
console.log(myNewURL.pathname); // Logs "/foo"
console.log(myNewURL.protocol); // Logs "https:"
See also
canParse
canParse(url: string | URL, base?: string | URL): booleanReturns a boolean value indicating if a URL string is valid and can be parsed.
Example
// Check if an absolute URL string is valid
console.log(URL.canParse('https://example.org')); // Logs true
console.log(URL.canParse('https:://example.org')); // Logs false
// Check if a relative URL string with a base is valid
console.log(URL.canParse('/foo', 'https://example.org')); // Logs true
console.log(URL.canParse('/foo', 'https:://example.org')); // Logs false
See also
createObjectURL
createObjectURL(blob: Blob): stringCreates a unique, temporary URL that represents a given Blob, File, or MediaSource object.
This method is particularly useful for:
- Creating URLs for dynamically generated content
- Working with blobs in a browser context
- Creating workers from dynamically generated code
- Setting up temporary URL references for file downloads
Note: Always call URL.revokeObjectURL() when you're done using the URL to prevent memory leaks.
Example
// Create a URL string for a Blob
const blob = new Blob(["Hello, world!"], { type: "text/plain" });
const url = URL.createObjectURL(blob);
console.log(url); // Logs something like "blob:null/1234-5678-9101-1121"
// Dynamic web worker creation in Ende
const workerCode = `
self.onmessage = (e) => {
self.postMessage(e.data.toUpperCase());
};
`;
const workerBlob = new Blob([workerCode], { type: "application/javascript" });
const workerUrl = URL.createObjectURL(workerBlob);
const worker = new Worker(workerUrl, { type: "module" });
worker.onmessage = (e) => console.log(e.data);
worker.postMessage("hello from ende");
// Always revoke when done to prevent memory leaks
URL.revokeObjectURL(workerUrl);
See also
revokeObjectURL
revokeObjectURL(url: string): voidRevokes a previously created object URL, freeing the memory associated with it.
Important for memory management in applications that create dynamic URLs. Once an object URL is revoked:
- It can no longer be used to fetch the content it referenced
- The browser/runtime is allowed to release the memory or resources associated with it
- Workers created via the URL will continue to run, but the URL becomes invalid for new creations
For security and performance in Ende applications, always revoke object URLs as soon as they're no longer needed, especially when processing large files or generating many URLs.
See also