簡體   English   中英

在社交媒體上從我的網站共享網頁時,如何縮短URL?

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

在社交媒體上從我的網站共享網頁時,如何縮短URL? 我希望可以使用javascript和api來完成,我已經檢查了一下api,但是不知道如何入門。 謝謝。

我為您提供2個有用的資源:

此網址適用於Google網址縮短程序: 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/

首先,您需要在http://bit.ly上有一個帳戶。 要創建一個,請轉到該站點並注冊。

注冊后,登錄並轉到“帳戶”頁面。 在那里,您會找到您的API密鑰。

將其放在您的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%;">

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM