在社交媒体上分享我网站上的网页时如何缩短 url?

How to shorten url when sharing a webpage from my website on social media?

在社交媒体上分享我网站上的网页时如何缩短 url? 我希望它可以用javascript和apis来完成,我已经检查了一下api,但不知道如何开始。 谢谢

我有 2 个对您有用的资源:

这个用于 google url 缩短器:http://www.i-visionblog.com/2014/07/google-url-shortener-api-javascript.html

而这个 bit.ly:https://bdhacker.wordpress.com/2010/03/30/dynamically-use-bitly-in-your-site-easiest-way/

First, you need an account on http://bit.ly. To create one, go to the site and register.

Once you’ve registered, login and go to your Account page. There, you will find your API key.

把这个放在你的 HTML 头上:

<script type="text/javascript" charset="utf-8" src="http://bit . ly/javascript-api.js?version=latest&login=******&apiKey=*****************"></script>

(删除 bit.ly url 中的空格。Whosebug 不允许 post 回答 url)


把这个放在</body>之前:

<script>
    // Bit.ly API
    BitlyCB.shortenResponse = function(data) {
            var sss = '';
            var first_result;
            // Results are keyed by longUrl, so we need to grab the first one.
            for     (var r in data.results) {
                    first_result = data.results[r]; break;
            }
            sss = first_result["shortUrl"].toString();
            document.getElementById("qlink").value = sss;
    }
    BitlyClient.shorten(window.location, 'BitlyCB.shortenResponse');
</script>

还有这个,在你页面的某处:

<h3>Link to this page</h3><br>
Use this link to tell others about this page! <input onclick = "this.select()" type = 'text' id = "qlink" style = "width:100%;">