完美优化百度自动推送JS代码适用自适应网站

图片[1]-完美优化百度自动推送JS代码适用自适应网站-轻刻年轮
自动推送 JS 代码 优化版来自百度站长学院

 

<script>
(function(){
var canonicalURL, curProtocol;
//Get the <link> tag
var x=document.getElementsByTagName("link");
//Find the last canonical URL
if(x.length > 0){
for (i=0;i<x.length;i++){
if(x[i].rel.toLowerCase() == 'canonical' && x[i].href){
canonicalURL=x[i].href;
}
}
}
//Get protocol
if (!canonicalURL){
curProtocol = window.location.protocol.split(':')[0];
}
else{
curProtocol = canonicalURL.split(':')[0];
}
//Get current URL if the canonical URL does not exist
if (!canonicalURL) canonicalURL = window.location.href;
//Assign script content. Replace current URL with the canonical URL
!function(){var e=/([http|https]:\/\/[a-zA-Z0-9\_\.]+\.baidu\.com)/gi,r=canonicalURL,t=document.referrer;if(!e.test(r)){var n=(String(curProtocol).toLowerCase() === 'https')?"https://sp0.baidu.com/9_Q4simg2RQJ8t7jm9iCKT-xh_/s.gif":"//api.share.baidu.com/s.gif";t?(n+="?r="+encodeURIComponent(document.referrer),r&&(n+="&l="+r)):r&&(n+="?l="+r);var i=new Image;i.src=n}}(window);})();
</script>

这个新脚本的作用是多了一步查看页面的canonical URL的步骤。我们知道canonical属性表示该页面纵有千种URL的花样,请搜索引擎只认准href中给出的URL值。这样一来就不会让搜索引擎为了同一个页面(或许已经索引了)多次检查你的推送页面具体内容。

这个版本好处在于推送的链接是 canonical 标签属性里面的链接,对于网站链接有过改版的网站是友好的,不会重复推送链接,上面的代码适用于任何网站!

使用这个改进版,百度默认的推送代码不能变,如果默认的自动推送代码改变,这个也会失效!

个人认为这个代码适用自适应站更好,因为自适应站手机版和PC版地址相同,
如果你的PC端与手机端访问域名不一致 建议不要使用,因为此优化代码不会推送移动端

购买后下载不了源码或源码功能失效请联系小编,确认后可立即退款,请勿无理投诉
© 版权声明
THE END
喜欢就支持一下吧
点赞0赞赏 分享
评论 共4条

请登录后发表评论