![](/img/trans.png)
[英]anchor href get current url and append on next page with javascript
[英]Get current page URL and append it to Facebook Share link (with javascript)
我一直在努力讓 Facebook 分享按鈕工作,但沒有運氣。 我想在我的所有頁面上使用相同的代碼,所以我需要能夠動態獲取當前頁面 url 並將其附加到 Facebook 共享 URL。
我能想到的最好的方法如下:
<a href="" onclick="window.open('http://www.facebook.com/share.php?u=' +
encodeURIComponent(location.href))">Share this page</a>
https://jsfiddle.net/08x2sufs/2/
但是,正如您從 jsFiddle 演示中看到的,該鏈接確實將我們帶到了 Facebook 共享頁面,但沒有要共享的頁面。
謝謝你的幫助。
我會使用以下方法:
<a href="" id="fb_share">Share this page</a>
<script>
window.onload = function() {
fb_share.href ='http://www.facebook.com/share.php?u=' + encodeURIComponent(location.href);
}
</script>
$Url = (@$_SERVER["HTTPS"] == "on") ? "https://" : "http://";
$Url .= $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
<div class="fb-share-button" data-href="<?php echo $Url; ?>" data-layout="button_count" data-size="small">
<a target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=<?php echo $Url; ?>&src=sdkpreparse" class="fb-xfbml-parse-ignore">Share</a></div>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.