问题
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