利用JS延遲加載百度分享代碼,提高網(wǎng)頁速度
更新時間:2013年07月01日 12:33:22 作者:
相信大家經(jīng)常在一些網(wǎng)站上看到有快捷分享到各大流行網(wǎng)站的按鈕,目前流行的有JiaThis、百度分享、Bshare等,目前用百度分享的居多
發(fā)現(xiàn)很多網(wǎng)站在放置百度分享代碼的時候,簡單的將分享代碼放置到固定的網(wǎng)頁位置就完事了,這是非常致命的方式。因為,我經(jīng)常打開一個網(wǎng)頁的時候,發(fā)現(xiàn)在網(wǎng)頁加載到分享代碼的時候,有時候花上幾秒的時候來請求百度的服務(wù)器,最后展示分享按鈕。
其實,像這樣對網(wǎng)頁來說不是非常重要的功能,我們大可以用JS來延遲加載,從而提高網(wǎng)頁主要內(nèi)容的快速加載顯示。
這里分享下我的放置方式。
一、copy百度分享代碼,如下:
<!-- Baidu Button BEGIN -->
<div id="bdshare" class="bdshare_t bds_tools_32 get-codes-bdshare">
<a class="bds_tsina"></a>
<a class="bds_qzone"></a>
<a class="bds_tqq"></a>
<a class="bds_renren"></a>
<a class="bds_douban"></a>
<span class="bds_more"></span>
<a class="shareCount"></a>
</div>
<script type="text/javascript" id="bdshare_js" data="type=tools&uid=0" ></script>
<script type="text/javascript" id="bdshell_js"></script>
<script type="text/javascript">
document.getElementById("bdshell_js").src = "http://bdimg.share.baidu.com/static/js/shell_v2.js?cdnversion=" + Math.ceil(new Date()/3600000)
</script>
<!-- Baidu Button END -->
二、放置代碼
認(rèn)真分析上面的分享代碼,我們可以發(fā)現(xiàn),其中有3個js腳本標(biāo)簽,這些都是有可能影響網(wǎng)頁呈現(xiàn)速度的,最后發(fā)現(xiàn),其實,只有最后一個<script>標(biāo)簽的作用是請求百度服務(wù)器,展示分享圖片和鏈接。那么,這條JS我們大可放到最后加載。
下面是我做的一個demo:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>JS延遲加載百度分享代碼,提高網(wǎng)頁速度</title>
<meta http-equiv="Content-Type" content="text/html;charset=gb2312" />
<style type="text/css">
body{margin:0px;padding:0px;font-size:12px;}
#copyright{clear:both;}
</style>
</head>
<body>
<div id="baidu-share">
<!-- Baidu Button BEGIN -->
<div id="bdshare" class="bdshare_t bds_tools_32 get-codes-bdshare">
<a class="bds_tsina"></a>
<a class="bds_qzone"></a>
<a class="bds_tqq"></a>
<a class="bds_renren"></a>
<a class="bds_douban"></a>
<span class="bds_more"></span>
<a class="shareCount"></a>
</div>
<script type="text/javascript" id="bdshare_js" data="type=tools&uid=0" ></script>
<script type="text/javascript" id="bdshell_js"></script>
<!-- Baidu Button END -->
</div>
<div id="copyright">
<a >編程圖書PDF下載【codejia.net】</a>
</div>
<script type="text/javascript">
window.onload = shareCode;
function shareCode(){
document.getElementById("bdshell_js").src = "http://bdimg.share.baidu.com/static/js/shell_v2.js?cdnversion=" + Math.ceil(new Date()/3600000);
}
</script>
</body>
</html>
分析:通過上面的demo,可以發(fā)現(xiàn)我只是單純的將最后一個有src屬性的script標(biāo)簽,放到最后動態(tài)加載的,并且是在window.onload之后加載。之前加載的都是純html標(biāo)簽代碼,并不會有多影響網(wǎng)頁速度。
百度分享代碼通過這樣調(diào)整后就算百度的服務(wù)器掛了,也不會影響自己的網(wǎng)頁正常顯示。
其實,這樣的技巧還有很多地方都可以用。想網(wǎng)站統(tǒng)計代碼、百度谷歌搜索代碼、第三方廣告代碼等,我們都可以放到網(wǎng)頁底部最后來加載,這樣即使第三方的服務(wù)器宕機(jī),我們自己的機(jī)器也不會受到多少影響。
其實,像這樣對網(wǎng)頁來說不是非常重要的功能,我們大可以用JS來延遲加載,從而提高網(wǎng)頁主要內(nèi)容的快速加載顯示。
這里分享下我的放置方式。
一、copy百度分享代碼,如下:
復(fù)制代碼 代碼如下:
<!-- Baidu Button BEGIN -->
<div id="bdshare" class="bdshare_t bds_tools_32 get-codes-bdshare">
<a class="bds_tsina"></a>
<a class="bds_qzone"></a>
<a class="bds_tqq"></a>
<a class="bds_renren"></a>
<a class="bds_douban"></a>
<span class="bds_more"></span>
<a class="shareCount"></a>
</div>
<script type="text/javascript" id="bdshare_js" data="type=tools&uid=0" ></script>
<script type="text/javascript" id="bdshell_js"></script>
<script type="text/javascript">
document.getElementById("bdshell_js").src = "http://bdimg.share.baidu.com/static/js/shell_v2.js?cdnversion=" + Math.ceil(new Date()/3600000)
</script>
<!-- Baidu Button END -->
二、放置代碼
認(rèn)真分析上面的分享代碼,我們可以發(fā)現(xiàn),其中有3個js腳本標(biāo)簽,這些都是有可能影響網(wǎng)頁呈現(xiàn)速度的,最后發(fā)現(xiàn),其實,只有最后一個<script>標(biāo)簽的作用是請求百度服務(wù)器,展示分享圖片和鏈接。那么,這條JS我們大可放到最后加載。
下面是我做的一個demo:
復(fù)制代碼 代碼如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>JS延遲加載百度分享代碼,提高網(wǎng)頁速度</title>
<meta http-equiv="Content-Type" content="text/html;charset=gb2312" />
<style type="text/css">
body{margin:0px;padding:0px;font-size:12px;}
#copyright{clear:both;}
</style>
</head>
<body>
<div id="baidu-share">
<!-- Baidu Button BEGIN -->
<div id="bdshare" class="bdshare_t bds_tools_32 get-codes-bdshare">
<a class="bds_tsina"></a>
<a class="bds_qzone"></a>
<a class="bds_tqq"></a>
<a class="bds_renren"></a>
<a class="bds_douban"></a>
<span class="bds_more"></span>
<a class="shareCount"></a>
</div>
<script type="text/javascript" id="bdshare_js" data="type=tools&uid=0" ></script>
<script type="text/javascript" id="bdshell_js"></script>
<!-- Baidu Button END -->
</div>
<div id="copyright">
<a >編程圖書PDF下載【codejia.net】</a>
</div>
<script type="text/javascript">
window.onload = shareCode;
function shareCode(){
document.getElementById("bdshell_js").src = "http://bdimg.share.baidu.com/static/js/shell_v2.js?cdnversion=" + Math.ceil(new Date()/3600000);
}
</script>
</body>
</html>
分析:通過上面的demo,可以發(fā)現(xiàn)我只是單純的將最后一個有src屬性的script標(biāo)簽,放到最后動態(tài)加載的,并且是在window.onload之后加載。之前加載的都是純html標(biāo)簽代碼,并不會有多影響網(wǎng)頁速度。
百度分享代碼通過這樣調(diào)整后就算百度的服務(wù)器掛了,也不會影響自己的網(wǎng)頁正常顯示。
其實,這樣的技巧還有很多地方都可以用。想網(wǎng)站統(tǒng)計代碼、百度谷歌搜索代碼、第三方廣告代碼等,我們都可以放到網(wǎng)頁底部最后來加載,這樣即使第三方的服務(wù)器宕機(jī),我們自己的機(jī)器也不會受到多少影響。
相關(guān)文章
layer.open屬性詳解及l(fā)ayer.open彈出框使用post方法舉例
這篇文章主要給大家介紹了關(guān)于layer.open屬性詳解及l(fā)ayer.open彈出框使用post方法的相關(guān)資料,最近接觸到layer彈窗,感覺彈窗功能異常強(qiáng)大,真的很方便,所以記錄下來,需要的朋友可以參考下2023-12-12JS獲取客戶端IP地址、MAC和主機(jī)名的7個方法匯總
這篇文章主要介紹了JS獲取客戶端IP地址、MAC和主機(jī)名的7個方法匯總,JS本身是不支持獲取IP地址等信息的,本文通過其它方法實現(xiàn),需要的朋友可以參考下2014-07-07JavaScript實現(xiàn)網(wǎng)頁圖片等比例縮放實現(xiàn)代碼及調(diào)用方式
為了保證圖片統(tǒng)一大小,直接設(shè)置圖片大小又會導(dǎo)致圖片拉伸,造成圖片模糊,接下來將介紹的代碼可以在圖片加載完成后自動按比例調(diào)整圖片大小,感興趣的你可以參考下2013-02-02javascript currying返回函數(shù)的函數(shù)
currying函數(shù)是一種返回函數(shù)的函數(shù),是閉包最偉大的應(yīng)用之一。有關(guān)閉包更詳細(xì)的定義可參見這里與這里。如下currying函數(shù)的一種定義。2009-11-11