Skip to content

Instantly share code, notes, and snippets.

@startswithaj
startswithaj / gist:6773174
Last active May 25, 2017 05:45
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)