JavaScript to achieve the final load of the Web page ads code (support Baidu, Google)

Source: Internet
Author: User

Let your website advertising no longer affect your site speed: First load the page after loading the advertising method, first load the page, after the loading of ads, advertising finally display, so as to ensure that the Web page of the normal access does not affect advertising! This advertising optimization method is applicable to all advertising framework, JS and so on!

The first step: put this code in the page where you want to put the ads, starters just show "ad loading ..." words

The code is as follows Copy Code

<div id=ad_box> AD Load ...</div>

The second step: put the code to the bottom of the page, the advertising code loaded to the top of the position, replacing the original text!

The code is as follows Copy Code

<div id= "Ad_code" style= "Display:none" > Place JS AD code or other slow-loading AD code </div>
<script type= "Text/javascript" >document.getelementbyid ("Ad_box"). Innerhtml=document.getelementbyid ("Ad_ Code ") .innerhtml;</script>

This setup later when entering the Web page in the advertising position is displayed in the ads ... And so on page display finished, the advertisement is shown after loading, if the advertisement server has a problem, will not because the advertisement affects the webpage visit!

Code compatibility Test passed: IE6/7/8, Firefox, Opera, Chrome, Safari

Not just Google ads can do so, like the Baidu Alliance ads or other forms of advertising can be loaded to enhance your Web experience.

Baidu AD Asynchronous loading

The Federation code does not currently have asynchronous loading, but the ad bit created by Baidu's ad Butler can also build federated materials and provide a variety of loading methods that can be used with the following code if the page has been loaded with jquery:

The code is as follows Copy Code

$.getscript ("Http://cbjs.baidu.com/js/m.js", function () {
Baidu_clb_fillslotasync ("12345", "your_id");
Baidu_clb_fillslotasync ("23456", "Your_id2");
});

If you do not load jquery, you can use the native asynchronous Load method

The code is as follows Copy Code

<script type= "Text/javascript" src= "Http://cbjs.baidu.com/js/m.js" ></script>
<script type= "Text/javascript" >
Baidu_clb_fillslotasync ("12345", "your_id");
Baidu_clb_fillslotasync ("23456", "Your_id2");
</script>

Of course, there is a situation, do not want to load jquery to use settimeout to do the countdown, you can refer to the Google Statistics code to write

  code is as follows copy code

    var cpro_id = "u123456";
   settimeout (function () {
     (function () {var du = document.createelement (' Script '); Du.type = ' Text/javascript '; DU.SRC = ' http://cpro.baidustatic.com/cpro/ui/c.js '; var s = document.getelementsbytagname (' script ') [0]; S.parentnode.insertbefore (Du, s);}) ();
  },1000);

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.