save bookmark with relative path to current-site

javascript:void(window.location.href = '/path-to-some-page');

Is another way of bookmarking a relative path.


I haven't seen a way to save a relative or root-relative link without resorting to a bookmarklet.

As far as a bookmarklet is concerned, it's relatively easy to generate one that will take you to whatever path you'd like:

javascript:(rel=>{location=rel.startsWith('/')?`${location.protocol}//${location.host}${rel}`:`${location.protocol}//${location.host}${location.pathname}/${rel}`})('/path')

Replace 'path' at the end with a properly escaped string containing whatever path you'd like. Note that this will differentiate between relative and root-relative paths based on whether they start with a / character.

In long form:

(rel => {
  location =
    // if the relative path starts with /
    rel.startsWith('/')
      // go to http(s)://{domain}/{relative path}
      ? `${location.protocol}//${location.host}${rel}`
      // otherwise go to http(s)://{domain}/{current path}/{relative path}
      : `${location.protocol}//${location.host}${location.pathname}/${rel}`
// call the function providing the relative path to use
})('/path')