<div id='ad' style='position:absolute; left : 174px; top : 14px; width : 597px; height : 740px;'>
<div style="opacity: 1.0;"> <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- 500-400 -->
<ins class="adsbygoogle"
     style="display:inline-block;width:500px;height:300px"
     data-ad-client="ca-pub-********"
     data-ad-slot="*****"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script> </div>
</div>
<script type='text/javascript'><!--
window.onscroll = function () { //hope this works, havent tested it
document.getElementById ('ad').style.top = (200 + document.body.scrollTop) + 'px';
}
--></script>

Dani AI

Generated

is right — this is not a browser window, so window.close() is not what you need. , the simplest, safest approach is to add a small "close" control inside the ad wrapper, hide or remove the wrapper when clicked, and optionally remember that choice with localStorage so it stays closed on reload.

Example (add the button to the container that wraps your ad markup):

<div id="ad" class="floating-ad">
  <button type="button" class="ad-close" aria-label="Close ad">X</button>
  <!-- existing ad markup goes here -->
</div>
/* minimal styles – adjust to match your layout */
#ad.floating-ad { position: fixed; top: 20px; right: 20px; z-index: 9999; transition: opacity .25s ease; }
.ad-close { position: absolute; top: 6px; right: 6px; border: 0; background: #333; color: #fff; padding: 4px 7px; cursor: pointer; }
/* hide + persist closed state */
(function(){
  var ad = document.getElementById('ad');
  if(!ad) return;
  if (localStorage.getItem('adClosed')) { ad.style.display = 'none'; return; }
  var btn = ad.querySelector('.ad-close');
  btn.addEventListener('click', function(e){
    e.preventDefault();
    ad.style.opacity = 0;
    setTimeout(function(){ if (ad.parentNode) ad.parentNode.removeChild(ad); }, 260);
    try { localStorage.setItem('adClosed','1'); } catch(e){}
  });
})();

Notes and troubleshooting:

  • Prefer position:fixed instead of constantly setting top on scroll; fixed is smoother and simpler. If you must animate position on scroll, throttle with requestAnimationFrame to avoid jank.
  • Keep the close button outside any third‑party ad tag (do not alter provider script tags). If the provider re-renders the ad element you may need the button on a stable parent wrapper or use a MutationObserver to reapply the closed state.
  • Add an aria-label and ensure the button is keyboard accessible so it is usable for all visitors.

This approach hides the element cleanly and avoids trying to "close a window", which is what was causing confusion earlier.

Recommended Answers

All 7 Replies

like that no like the script Capture.PNG

Martin, you can view source to see how a web site does something. I looked at that link and did not get a pop up window or such. So my advice is to read the source of that site to see what they did.

ok thnx you

I wish you could reveal what it is we want to close. I tried your script with a fiddle (online edit and test) but it went nowhere. Since we can't close a window, then it must be something else that is popping up. Once we know what that is, then we can go over its controls.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.