是否可以在页面加载期间获取在document.ready上创建的变量,并在页面加载完成时将其吐出到文字或标签标签中?到目前为止,我还没能抓住它。
答案 0 :(得分:0)
试试这个
<强> HTML 强>
<body>
Here's a string, inside it is the ad client: <span id="ad_client">ca-pub-<PUB_ID_HERE></span>, and the ad slot: <span id="ad_slot"><AD_SLOT_HERE></span>
</body>
<强> JS 强>
var ad_client = document.getElementById('ad_client').value;
var ad_slot = document.getElementById('ad_slot').value;
// Create the <script> element
var script = document.createElement('script');
script.src = 'https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js';
script.attributes.async = true;
document.body.appendChild(script);
// Create the <ins> element
var ins = document.createElement('ins');
ins.className = 'adsbygoogle';
ins.style = 'display:inline-block;width:728px;height:90px';
ins.dataset.adClient = ad_client;
ins.dataset.adSlot = ad_slot;
document.body.appendChild(ins);
setTimeout(function() {
(adsbygoogle = window.adsbygoogle || []).push({});
}, 250)
请注意,没有setTimeout,它对我来说很好,但是作为预防措施将它放在那里。最好在回调中做到这一点。