JavaScript code Load Optimization method _javascript Tips

Source: Internet
Author: User
Here's an example of 1 simpler ways to do this:

We used to save the statistics code to 1 files: File path:/config/counter.conf

The statistical code is as follows:
Copy Code code 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 contents of the file, the code will not be listed in detail

We need a page to output this JavaScript code:

Page:/do.ashx?args=getcounter
Copy Code code as follows:

String code = "Read the statistic Code";
Code = Regex.Replace (Code, "[\]", "\");
Code = Regex.Replace (Code, "[\n\r]", "");
Context. Response.Write ("document.write (' +code+ ');");

This will be able to add the output of JavaScript to the page to implement the statistics function!

All we need to do is add the following code to the JavaScript file that the Web page references:
Copy Code code 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);

Done, the statistics code will not be displayed on your page, but in fact it has been loaded into your Web page!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.