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:

  •''+location.href, '','width=750,height=500,scrollbars=yes');

Here we’re opening a new window, and using the 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 immediately puts the new shortened URL onto your clipboard for ...

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

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.