A link should behave like a link!


/ Published in: JavaScript
Save to your folder(s)

/*
* There’s a lot of awesome functionality built into linking elements like <a> and .
* If you middle click or command-click on them they’ll open in new windows.
* When you hover over an <a> your browser tells you the URL in the status bar.
* Don’t break this behavior when playing with onReplaceState and onPushState.
* Embed the location of AJAX requests in the href attributes of anchor elements.
* Return true from Javascript click handlers when people middle or command click.
*
* http://warpspire.com/posts/url-design/ */


Copy this code and paste it in your HTML
  1. $('a.ajaxylink').click(function(e){
  2. // Fallback for browser that don't support the history API
  3. if (!('replaceState' in window.history)) return true
  4.  
  5. // Ensure middle, control and command clicks act normally
  6. if (e.which == 2 || e.metaKey || e.ctrlKey){
  7. return true
  8. }
  9.  
  10. // Do something awesome, then change the URL
  11. window.history.replaceState(null, "New Title", '/some/cool/url')
  12. return false
  13. })

URL: http://warpspire.com/posts/url-design/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.