javascript代碼加載優(yōu)化方法
更新時間:2011年01月30日 01:12:26 作者:
給你的網(wǎng)站加上代碼統(tǒng)計!常用的方法是直接加統(tǒng)計代碼到網(wǎng)頁,但你的網(wǎng)頁數(shù)量很多呢?
下面我們通過這個例子介紹1個更簡單的方法:
我們用將統(tǒng)計代碼保存到1個文件:文件路徑:/config/counter.conf
統(tǒng)計代碼如下:
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-18744406-1']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga,s);
})();
</script>
我們用StreamReader將文件內(nèi)容讀出來,代碼將不詳細(xì)列出
我們需要一個頁面來輸出這段javascript代碼:
頁面:/do.ashx?args=GetCounter
string code = "讀取到的統(tǒng)計代碼";
code = Regex.Replace(code, "[\']","\"");
code = Regex.Replace(code, "[\n\r]", "");
context.Response.Write("document.write('"+code+"');");
這樣就能將輸出的javascript添加到頁面實現(xiàn)統(tǒng)計功能了!
我們只需在網(wǎng)頁都引用的javascript文件中添加如下代碼:
var _s=document.createElement('script');
_s.type='text/javascript';
_s.src='/do.ashx?args=GetCounter';
var _fs=document.getElementsByTagName("script")[0];
_fs.parentNode.insertBefore(_s,_fs);
大功告成,統(tǒng)計代碼不會顯示在你的網(wǎng)頁中,但事實上卻已經(jīng)加載到了你的網(wǎng)頁!
我們用將統(tǒng)計代碼保存到1個文件:文件路徑:/config/counter.conf
統(tǒng)計代碼如下:
復(fù)制代碼 代碼如下:
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-18744406-1']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga,s);
})();
</script>
我們用StreamReader將文件內(nèi)容讀出來,代碼將不詳細(xì)列出
我們需要一個頁面來輸出這段javascript代碼:
頁面:/do.ashx?args=GetCounter
復(fù)制代碼 代碼如下:
string code = "讀取到的統(tǒng)計代碼";
code = Regex.Replace(code, "[\']","\"");
code = Regex.Replace(code, "[\n\r]", "");
context.Response.Write("document.write('"+code+"');");
這樣就能將輸出的javascript添加到頁面實現(xiàn)統(tǒng)計功能了!
我們只需在網(wǎng)頁都引用的javascript文件中添加如下代碼:
復(fù)制代碼 代碼如下:
var _s=document.createElement('script');
_s.type='text/javascript';
_s.src='/do.ashx?args=GetCounter';
var _fs=document.getElementsByTagName("script")[0];
_fs.parentNode.insertBefore(_s,_fs);
大功告成,統(tǒng)計代碼不會顯示在你的網(wǎng)頁中,但事實上卻已經(jīng)加載到了你的網(wǎng)頁!
相關(guān)文章
學(xué)習(xí)javascript面向?qū)ο?理解javascript原型和原型鏈
這篇文章主要介紹了javascript原型和原型鏈,學(xué)習(xí)javascript面向?qū)ο?,感興趣的小伙伴們可以參考一下2016-01-01微信小程序自定義組件傳值 頁面和組件相互傳數(shù)據(jù)操作示例
這篇文章主要介紹了微信小程序自定義組件傳值 頁面和組件相互傳數(shù)據(jù)操作,結(jié)合實例形式分析了微信小程序常見傳值操作相關(guān)實現(xiàn)技巧,需要的朋友可以參考下2019-05-05