Implementation code of alternative js call of google advertisement

  • 2021-08-06 20:42:06
  • OfStack

Today, I found an alternative call method for google advertisement


function addGoogel (content, obj) {
 addScript('//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js', function () {
  var ins = document.createElement('ins')
  ins.setAttribute('class', 'adsbygoogle')
  ins.setAttribute('data-ad-client', 'ca-pub-******')
  ins.setAttribute('style', obj.style)
  ins.setAttribute('data-ad-slot', obj.slot)
  content.appendChild(ins)
  var inlineScript = document.createElement('script')
  inlineScript.type = 'text/javascript'
  inlineScript.text = '(adsbygoogle = window.adsbygoogle || []).push({});'
  document.getElementsByTagName('body')[0].appendChild(inlineScript)
 })
}

Invoke method


window.onload = function () {
 var line = document.getElementById('ad_line'), //  Banner 
  area = document.getElementById('ad_area')//  Matrix 
 if (!isYd()) { //  Not for mobile terminal 

  if (line) { // Banner 
   addGoogel(line, {
    style: 'display:inline-block;width:1100px;height:160px',
    slot: '6183651273'
   })
 }
  if (area) { // Rectangle 
   addGoogel(area, {
    style: 'display:inline-block;width:250px;height:250px',
    slot: '1995310048'
   })
  }
 }
}

Related articles: