Home > Web Front-end > JS Tutorial > javascript loads after the advertisement, loads the advertisement after loading the page_javascript skills

javascript loads after the advertisement, loads the advertisement after loading the page_javascript skills

WBOY
Release: 2016-05-16 18:15:26
Original
1450 people have browsed it

The principle of loading the page first and then loading the advertisement:

The order in which web pages are opened is loaded from top to bottom. Therefore, if you want ads to not affect the page opening speed, you must wait until all pages are opened before loading the JS code. Waiting for the page to load before loading the ad is actually to place the ad's JS code at the bottom of the page, wait until the page content is opened, then load it into the ad code, and then use the reserved DIV on the page to output the ad.

How to use:
1. Place the following code where you want to place ads on the page

Copy code The code is as follows:

Loading...


2. Place the following code at the bottom of the page, which is the footer and the bottom of the php file.
Copy code The code is as follows:

Put ad code here




OK, this way, the page is loaded first and then the advertisement is loaded. How about not affecting everyone’s reading?

The above method can load any type of advertisement, but now Baidu and Google do not allow it to be used. It will easily cause the advertisement to be loaded twice and the click-through rate will also be reduced. Below, the editor of Script House will share with you a relatively new way of loading ads:

1. Can load string type and google type

<div id="logo_m"></div>
<script>
function doad(datastr,id){ 
//getid(id).innerHTML = datastr; 这个是原生js的写法
$("#"+id).html(datastr); //这个是jquery写法
}
/*
上面的都是直接的字符串写入,不能是js代码
下面的代码利用的是jquery的append加载google广告不错,百度的好像不能用。后加载百度的可以是用百度管家自带的函数。
*/
function doad2(datastr,id){ 
$("#"+id).append(datastr);
}

var logo_m='<a href="http://www.danhw.com/" target="_blank"><img src="http://files.jb51.net/image/kongbao_370.gif" width=370 height=60 /></a>';
doad(logo_m,'logo_m');
</script>
Copy after login

2. Code that can load Google ads

$(document).ready(function(){
	$('#google_ads_1').append("" +
	"<script src=\"\/\/pagead2.googlesyndication.com/pagead/js/adsbygoogle.js\"><\/script>"+
	"<!-- question-side -->"+
	"<ins class=\"adsbygoogle\""+
	"   style=\"display:inline-block;width:728px;height:90px\""+
	"   data-ad-client=\"ca-pub-345345345345\""+
	"   data-ad-slot=\"345345345\"></ins>"+
	"<script>"+
	"  (adsbygoogle = window.adsbygoogle || []).push({});"+
	"<\/script>");
});
Copy after login

Usually, the page is completely loaded, and ads are added. Of course, Google’s new code for asynchronously loading ads is also very fast, and the web page will not be stuck.
The above are all asynchronous advertising codes that load Google later.

I personally don’t like this character loading method, but I still prefer it. The code is as follows

var botad='<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>';
botad+='<ins class="adsbygoogle" style="display:inline-block;width:336px;height:280px" data-ad-client="ca-pub-343534534" data-ad-slot="6343459"></ins><script>(adsbygoogle = window.adsbygoogle || []).push({});</script>';

$(document).ready(function(){
	$('#google_ads_1').append();
});
Copy after login

3. You can load the code of Baidu Advertising Alliance
Baidu Butler’s own code for post-loading ads, based on jquery

<div id="tonglanbd"></div>
<script type="text/javascript">
$.getScript("http://cbjs.baidu.com/js/m.js", function() {
BAIDU_CLB_fillSlotAsync("广告id,不能带u","tonglanbd");
});
</script>
Copy after login

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template