A couple of useful snippets for web developing. The list will grow over the years.
Jumping to anchors with JS
/* dont use this!!! */ //location.href = location.href + '#searchbox'; /* and not this either: */ //location.href="#searchbox"; /* this is the correct way to go */ window.location.hash = "searchbox";
This has still some flaws if you click the anchor multiple times.
This worked for me:
var destinationLink = document.getElementById('targetId'); var destx = destinationLink.offsetLeft; var desty = destinationLink.offsetTop; scrollTo(destx, desty);
So the above method should be used to set the hash of the page. The below one for jumping to anchors.
Prevent Right Click
This should be used wisely and only where it makes sense. It it neither waterproof nor convenient in most cases. Simply by displaying the source code the content can still be accessed (Shortcuts like STRG+U).
<body oncontextmenu="return false"> ... </body>
I like the browser solution more than the js solutions out there 🙂
Get HTML5 ready
<input name="url" placeholder="http://"/>
This is only one example of many. The Google Chrome browser already understands it as well as some beta browsers like FF4 etc.
And some more snippets from different external sources: