在社交媒体上分享我网站上的网页时如何缩短网址? 我希望它可以用 javascript 和 apis 来完成,我检查了 bitly api,但不知道如何开始。 谢谢。
最佳答案
我有 2 个有用的资源给你:
这个用于谷歌网址缩短器: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 HEAD 中:
<script type="text/javascript" charset="utf-8" src="http://bit . ly/javascript-api.js?version=latest&login=******&apiKey=*****************"></script>
(删除 bit.ly url 中的空格。stackoverflow 不允许使用该 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%;">
关于javascript - 在社交媒体上分享我网站上的网页时如何缩短网址?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40419250/