Shortening URLs

There are a number of reasons why you might want a shorter version of a URL than the one of the page that you’re currently on-maybe you’re using a Twitter-like service with a short number of characters allowed, or maybe you’re going to paste the result into an email and you don’t want it to wrap. Either way, Script 17.12 makes it simple.

To shorten URLs:

  • window.open('http://tinyurl.com/create.php?url='+location.href, '','width=750,height=500,scrollbars=yes');

Here we’re opening a new window, and using the TinyURL.com service to set our short URL. We pass it our current page location (found in location.href) and that’s all the information it needs. Because TinyURL.com immediately puts the new shortened URL onto your clipboard for ...

Get JavaScript and Ajax for the Web: Visual QuickStart Guide, Seventh Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.