Skip to content

Instantly share code, notes, and snippets.

@donohoe
Forked from 140bytes/LICENSE.txt
Created January 4, 2012 17:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save donohoe/1561193 to your computer and use it in GitHub Desktop.
Save donohoe/1561193 to your computer and use it in GitHub Desktop.
Dynamic Anchor links in (exactly) 140 bytes

Note that this snippet has to run after DOM has loaded for it to work — the easiest way to test is to copy and paste it into the JavaScript console after appending "#/" (without quotes) and followed by a integar to the URL.

Do what you want with this code, but please don't use it. Use Emphasis - Dynamic Deep-Linking and Highlighting which is more robust, offers highlighting and will work even if you reorder and alter content.

function(){
// Get the Paragraph number from the URL, lowest value can be 0
// The expected format for the 6th paragraph is: http://example.com/folder/#/5
var pn = parseInt(location.hash.replace("#/", ""), 10);
if (!isNaN(pn)) {
var ps = document.querySelectorAll("p"); // Get a list of all the Paragraphs in the page
if (pn < ps.length) {
ps[pn].scrollIntoView(true); // Jump to the Paragraph
}
}
}
function(){var n,p;n=parseInt(location.hash.replace("#/",""),10);if(!isNaN(n)){p=document.querySelectorAll("p");if(n<p.length){p[n].scrollIntoView(1)}}}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 YOUR_NAME_HERE <YOUR_URL_HERE>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
{
"name": "dynamicAnchorLinks",
"description": "Link to any Paragraph in your web page with dynamic anchor links",
"keywords": [
"140bytes",
"paragraph",
"anchor",
"link",
"dynamic"
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment