Sharing Page Anchors on Social Networks

末鹿安然 提交于 2019-12-25 05:32:28

问题


I'm using Wordpress to create a single paged site which uses anchors on the page for navigation, however when sharing these links all social networks appear to strip out the anchor portion of the URL.

Is there an easy way around this?

Alternatively could anyone provide PHP or JavaScript code I could include on my Wordpress site to be able to point URL arguments links to named anchors. e.g.

http://example.com/?a=anchor redirecting to: http://example.com/#anchor

Thanks!


回答1:


I ran into the same exact issue you describe where the anchor links on my one page site were being stripped out when I tried to share specific links using a social media sharing plugin. I ended up using Javascript to replace the hashtags with %23, like so:

$(document).ready(function(){
    $('.social-media-links a').each(function(){
        this.href = this.href.replace('#', '%23');
    });
});

And that worked perfectly for me, now my hashtags and therefore my anchor links stay intact when people try to share them by clicking a "share" button on the website.

Here is a link to the blog post I wrote about my solution - http://icode4you.net/how-to-preserve-anchor-tags-in-links-shared-using-a-social-media-sharing-plugin-on-a-one-page-website/.



来源:https://stackoverflow.com/questions/19905244/sharing-page-anchors-on-social-networks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!