简体   繁体   English

获取当前页面 URL 并将其附加到 Facebook 共享链接(使用 javascript)

[英]Get current page URL and append it to Facebook Share link (with javascript)

I've been trying really hard to make the Facebook share button work, but no luck.我一直在努力让 Facebook 分享按钮工作,但没有运气。 I'd like to have the same code on ALL my pages, so I need to be able to dynamically get the current page url and append it to the Facebook share URL.我想在我的所有页面上使用相同的代码,所以我需要能够动态获取当前页面 url 并将其附加到 Facebook 共享 URL。

The best I could come up with is the following:我能想到的最好的方法如下:

<a href="" onclick="window.open('http://www.facebook.com/share.php?u=' + 
encodeURIComponent(location.href))">Share this page</a>

https://jsfiddle.net/08x2sufs/2/ https://jsfiddle.net/08x2sufs/2/

However, as you can see from the jsFiddle demo, the link does take us to a Facebook share page, but there is no page to share.但是,正如您从 jsFiddle 演示中看到的,该链接确实将我们带到了 Facebook 共享页面,但没有要共享的页面。

Thank you for your help.谢谢你的帮助。

I would use the following approach:我会使用以下方法:

<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; ?>&amp;src=sdkpreparse" class="fb-xfbml-parse-ignore">Share</a></div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM