Javascript: ad loading: loading Baidu Google alliance ads [original] _ javascript skills

Source: Internet
Author: User
This article mainly introduces a new post-ad loading method that supports custom HTML ads, Baidu alliance ads, and Google alliance ads. This method is executed after the page is loaded without affecting the display of the content, which is more user-friendly. This article mainly introduces a new post-ad loading method that supports custom HTML ads, Baidu alliance ads, and Google alliance ads. This method is executed after the page is loaded without affecting the display of the content, which is more user-friendly.

When we place advertisements on the website, the simplest way is to insert the JS code into the specified location. The consequence is that the page is loaded in order, and occasionally an advertisement code gets stuck, the whole page will be stuck, and the user experience is very poor.

How can this problem be solved? First, let's talk about the principle. we reserve some placeholders on our pages. in order not to affect the loading of page content, we introduce JS processing at the bottom of the page and replace the placeholders with corresponding advertisements one by one.

The specific implementation steps are as follows:

1. placing placeholders on the page is actually a span tag

The code is as follows:





2. Compile an independent JS script code: jbLoader. js

The code is as follows:


JbMap = window. jbMap || {};
Function jbViaJs (locationId ){
Var _ f = undefined;
Var _ fconv = 'jbmap [\ "'+ locationId +' \"] ';
Try {
_ F = eval (_ fconv );
If (_ f! = Undefined ){
_ F ()
}
} Catch (e ){}
}
Function jbLoader (closetag ){
Var jbTest = null,
JbTestPos = document. getElementsByTagName ("span ");
For (var I = 0; I <jbTestPos. length; I ++ ){
If (jbTestPos [I]. className = "jbTestPos "){
JbTest = jbTestPos [I];
Break
}
}
If (jbTest = null) return;
If (! Closetag ){
Document. write ("");
JbViaJs (jbTest. id );
Return
}
Document. write ("");
Var real = document. getElementById ("jbTestPos _" + jbTest. id );
For (var I = 0; I <real. childNodes. length; I ++ ){
Var node = real. childNodes [I];
If (node. tagName = "SCRIPT" &/closetag/. test (node. className) continue;
JbTest. parentNode. insertBefore (node, jbTest );
I --
}
JbTest. parentNode. removeChild (jbTest );
Real. parentNode. removeChild (real)
}

JbMap ['ads _ one'] = function (){
Document. writeln ('foots ');
};

JbMap ['ads _ two'] = function (){
Document. writeln (' Var cpro_id = "u336546"; script

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.