繁体   English   中英

在 Adsence 的另一个 html 文件中包含 html

[英]Include html in another html file for Adsence

我正在尝试从位于服务器主文件夹中的名为 sda.html 的文件中包含我的 Adsense 代码。

我正在使用此代码来包含它:

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

从这个来源: https://www.w3schools.com/howto/howto_html_include.asp

但我觉得有问题。

顺便说一句,我的网站只有 HTML 和 js,所以如果有任何其他更好的选择,我会很高兴听到它。

我还检查了这个:

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

但我没有使用它,因为我不知道我的下一个服务器将如何运行。 所以我跳过它。

这个在这里:

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

我更喜欢这个,但我无法控制它的外观,我无法居中或其他任何东西

代码越小越好。

sda.html是否仅包含广告代码或页面的整个部分?

w3school 的includeHTML function 不是很好。 我怀疑您遇到的问题是 function 使用innerHTML来设置内容,而innerHTML不执行带有内容的<script>标记:检查 MDN 页面上的“安全注意事项”。

要解决此问题,您可以执行以下操作:从sda.html中删除<script>标签,然后在导入 HTML 后,为每个新广告单元运行window.adsbygoogle.push({}) 例子:

  1. 在您的页面中添加 adsbygoogle.js 标记:
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
  1. 将广告插入 id 为div-with-ad的 div 中。
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({});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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