简体   繁体   中英

Include html in another html file for Adsence

I'm trying to include my Adsense code from a file called sda.html located in the home folder of the server.

I'm using this code to include it:

    <div w3-include-html="../../sda.html" class="section_title text-center"></div>

from this source: https://www.w3schools.com/howto/howto_html_include.asp

but idk I feel there's something wrong.

btw my site is only HTML and js, so if there is any other better option I'll be glad to hear it.

I also checked this one down:

<!--#include virtual="/menu.shtml" -->

but I didn't use it, since I have no clue how my next server will operated. so I skip it.

and this one here:

<object data="../../sda.html"></object>

I prefer this one, but I have no control of the look of it, I couldn't center or anything

the smaller the code the better it is.

Does sda.html contains only adsense code or whole part of the page?

The includeHTML function from w3school is not very good. I suspect the issue you are having is that that function uses innerHTML to set content and innerHTML doesn't execute <script> tags with content: check "Security considerations" on MDN page .

To workaround this you can do the following: remove <script> tags from sda.html and then, once you imported HTML run window.adsbygoogle.push({}) for each new ad unit. Example:

  1. Add adsbygoogle.js tag in of your page:
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
  1. Insert ad into a div with id div-with-ad .
const divWithAd = document.querySelector('#div-with-ad');
divWithAd.innerHTML = `
   <ins class="adsbygoogle"
     style="display:block; text-align:center;"
     data-ad-format="fluid"
     data-ad-layout="in-article"
     data-ad-client="ca-pub-0123456789101112"
     data-ad-slot="9876543210"></ins>
  `;
adsbygoog.push({});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM