下面的代码是需要注入到Sovrn显示广告的头部的。
<script type="application/javascript">var googletag=googletag||{};googletag.cmd=googletag.cmd||[];googletag.cmd.push(function(){googletag.pubads().disableInitialLoad()});</script><script type="application/javascript" src="//ap.lijit.com/www/headerauction/headersuite.min.js?configId=XXXX"></script>
它寻找一个ID="21728840 "的div来显示该div的广告。我使用的是Netlify,所以将这个脚本注入头部不是问题,但Netlify会进行后处理。因此,当脚本加载的时候,它无法找到ID,因为这个元素没有定义,并返回一个403错误。我看了很多建议,并尝试在广告所在的文件中使用Effect是这样的。
export const BlogIndexTemplate = ({ posts, title...
...
useEffect(() => {
var googletag = googletag || {}
googletag.cmd = googletag.cmd || []
googletag.cmd.push(function() {
googletag.pubads().disableInitialLoad()
})
const script = document.createElement('script')
script.async = true
script.src =
'//ap.lijit.com/www/headerauction/headersuite.min.js?configId=XXXX'
document.head.appendChild(script)
}, [])
...
{!!posts.length && (
<section className="section">
<div className="container">
<PostSection posts={filteredPosts} />
</div>
</section>
)}
广告容器位于PostSection中,就像这样。
class PostSection extends React.Component {
...
<div className="sidebar-sticky-container">
<div className="ad-skyscraper-container sticky-widget">
<div id="21728840"></div>
我不知道还有什么其他的方法可以让脚本被注入,并操纵ID为21728840的div来显示广告。