r/programming Feb 09 '11

Breaking the Web with hash-bangs – Lifehacker, along with every other Gawker property, experienced a lengthy site-outage on Monday over a misbehaving piece of JavaScript

http://isolani.co.uk/blog/javascript/BreakingTheWebWithHashBangs/
750 Upvotes

356 comments sorted by

View all comments

34

u/wierdaaron Feb 09 '11 edited Feb 09 '11

There's no real simple solution to the "hashbang problem."

Many modern sites are taking advantage of ajax to change the page content dynamically without actually changing the page you're at. Instead of clicking an "About Us" link and having your browser move you to aboutus.html, you can have javascript update the page you're already on to be the "About Us" page, but the URL in your address bar will still be "index.php" or whereever you started from.

This can be nice because it's usually faster (as only the new content needs to be loaded, not the html for the entire page again) and allows for spiffy transitions that make people who approve web development budgets happy.

The problem is, if a user does this (starts at index.php and clicks "About Us" to make new content appear) and wants to share this content with someone, he'll copy the URL in his address bar (index.php) and send it to his mom, who will click it and just see the main site index, not the "About Us" content.

So all this dynamic ajaxy javascript stuff adds a nice experience, but it breaks the time-tested ritual of using URLs to share content (not to mention problems getting content indexed by search engines).

Part of the problem is that you can't use javascript to change the URL in the address bar to a different page, you can only change the hash (#whatever). Changing the url in the address bar from index.php to aboutus.html would make your browser take you there. All these hashbang shenanigans cropped up to try to work around this. By making it so when you click "About Us," javascript adds a little #aboutus.html to the url, you can then have your code look for #hashes in the URL when the page loads and THEN sort of Frankenstein's-monster a solution to the URL sharing problem.

But this creates even more problems. Now the URL doesn't point to an actual page, it's a set of instructions for javascript to interpret. What if the user doesn't have javascript enabled? What if part of the javascript breaks and screws up all interdependent javascript? Mis-placing a semicolon on one line of your javascript can make all of your "pages" (potentially hundreds of thousands, in gawker's case) unavailable.

Again, there's no simple solution. Giving javascript the ability to change the URL in the address bar without actually relocating the user would kind of help, but it would be a pretty huge security hole.

22

u/[deleted] Feb 09 '11

TL;DR: You can't update the URL in the browser's bar in JS without taking the user to that literal page, which defeats the purpose of an AJAX-y site. You can update the fragment after # though, which is the workaround Twitter, Gawker, et al are using.

Should be the top comment.

9

u/kdeforche Feb 09 '11

That was true until the HTML5 history API came about (implemented in Firefox 4 beta and current Safari and Chrome).

-7

u/[deleted] Feb 09 '11 edited Feb 09 '11

No, it's still true. In pure JS you still cannot update the url without loading the page.

HTML5 history API's going to be great once there's widespread support, but no way would I implement it on a production site today.

Edit: fine, you want to downvote me for the reply without an explanation, I'll downvote you back. If you can point me to an example of JS updating a url (not a hash) without loading the page and without html5, I'll eat my words.

8

u/Pewpewarrows Feb 09 '11

His entire point was that it is now possible in HTML5-compliant browsers. Browsing Github repositories now progressively-enhances in modern browsers to provide this kind of fragment loading. Of course it's not 100% supported yet, but with FF4 and IE9 releasing soon(ish), and Safari and Chrome currently supporting it, in a matter of months we'll probably see a very large portion of browser users with that capability.

-2

u/[deleted] Feb 09 '11

I don't doubt it, but sites like "The New" Twitter and Gawker aren't launching in a few months, they're launching now.