How to call Google AdSense ads using JS files

888u

Last update at :2024-05-08,Edit by888u

Insert Google Adsense ads into web pages, generally embedding the source code directly into the theme template page. In fact Google AdSense supports and allows calls using JS files. Of course, the premise is that you do not modify the code for any reason, or manually affect the positioning of the ads.

For ordinary HTML static websites, the general approach is to put the public header and public trailer into separate JavaScript files, such as header.js and footer.js, and then edit these two JS files separately. Modify all website content at the same time, so we can consider putting the Google AdSense code into this header file. The specific steps are as follows:

First, log in to the Google AdSense backend and obtain the advertising code. Usually the code is as follows:

After that, we can put the following JavaScript code into the header.js header file. The code is as follows.

var js = document.createElement("script");

js.setAttribute(‘data-ad-client’, ‘ca-pub-YOUR-ID’);

js.setAttribute(‘async’, true);

js.setAttribute(‘src’, ‘https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js’);

document.head.appendChild(js);

In the above code, ca-pub-YOUR-ID needs to be replaced with the user's own ID number.

This code is exactly the same as the original Google AdSense JavaScript, and the actual display effect is the same.

Recommended site searches: domain name registration official website, cloud server rental, agent filing, domain name space purchase, dynamic IP vps, .cn domain name registration, high-defense server rental, US attack-proof server, Hong Kong server rental, vps foreign server,

How to call Google AdSense ads using JS files

All copyrights belong to 888u unless special state
取消
微信二维码
微信二维码
支付宝二维码