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