Skip to content

Instantly share code, notes, and snippets.

@startswithaj
Last active May 25, 2017 05:45
Show Gist options
  • Save startswithaj/6773174 to your computer and use it in GitHub Desktop.
Save startswithaj/6773174 to your computer and use it in GitHub Desktop.
Bookmarklet for generating link with anchor to selected (highlighted) text by finding the nearest element with an ID by traversing up the dom tree until one is found. Allows you to send links to URL's with a rough anchor to a point on a page.
javascript:z = function(x){ return x != null ? x.id != "" && x.id != null ? x.id : z(x.previousElementSibling || x.parentElement) : null}; window.prompt((y=z(window.getSelection().focusNode.parentElement)) != null ? "Copy the below link with " + y + " anchor" : "Cound not find anchor id",y != null ? window.location.href+"#"+y : ""); void(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment