The following example shows a simpler method:
Save the Statistical Code to one file: file path:/config/counter. conf.
The Statistical Code is as follows:Copy codeThe Code is as follows: <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>
We use StreamReader to read the file content, and the code will not be listed in detail.
We need a page to output this javascript code:
Page:/do. ashx? Args = GetCounterCopy codeThe code is as follows: string code = "Statistical code read ";
Code = Regex. Replace (code, "[\ ']", "\" ");
Code = Regex. Replace (code, "[\ n \ r]", "");
Context. Response. Write ("document. write ('" + code + "');");
In this way, the output javascript can be added to the page to implement the statistical function!
You only need to add the following code to the javascript file referenced on the webpage:Copy codeThe Code is as follows: 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 );
As a result, the statistical code is not displayed on your webpage, but has actually been loaded to Your webpage!