Redirect to another page
Navigate to a URL, adding an entry to the history.
function goTo(url) { location.assign(url); }
Usage
goTo('#pricing');
location.hash; // → '#pricing'
Know the limits
- A URL taken from user input is an open redirect, and a javascript: URL runs code. Accept only relative paths or an allow-list of origins.
- location.assign adds a history entry; location.replace does not. The test uses a fragment, which navigates within the page.
The function above is the code the usage lines run. It does not validate its arguments; add checks where it receives untrusted input.
Source & maintenance
Based on Redirect to another page from the original 1loc collection by Nguyen Huu Phuoc. Copyright (c) 2020 phuoc-ng. MIT licensed.
Uses location.assign; open-redirect and javascript: URL risks documented. Executed in Chromium with a fragment URL. Original MDX retained as inert source evidence.
Maintained by Oveyon. Source commit: 14b481cf60df. Read the license.
Keep exploring
- Check if a given DOMRect is contained within another DOMRect — True when the target lies inside the container or on its edges.
- Check if two DOMRects intersect — Two rects intersect when they meet on both axes.
- Compare two DOMRects for size and position match — Compare x, y, width and height exactly.