jQuery on the fly URL shortener

后端 未结 6 1687
迷失自我
迷失自我 2020-12-02 17:38

I\'m looking for an on the fly URL shortener much like how tweetdeck works. I have found many jQuery and general javascript plugins that take a url and run it through a shor

6条回答
  •  旧巷少年郎
    2020-12-02 18:10

    I guess the API of Bitly has changed slightly. You now only really need an access token to request a short URL.

    Following the best practices, I created the following Javascript-only snippet:

    getShortUrl: function(url, callback)
    {
       var accessToken = '___YOUR_ACCESS_TOKEN___';
       var url = 'https://api-ssl.bitly.com/v3/shorten?access_token=' + accessToken + '&longUrl=' + encodeURIComponent(url);
    
        $.getJSON(
            url,
            {},
            function(response)
            {
                if(callback)
                    callback(response.data.url);
            }
        );
    },
    

提交回复
热议问题