JS - change iframe body style for vanilla and jQuery

0 votes
193 views
added May 15, 2024 in Javascript by lcjr First Warrant Officer (11,940 points)
recategorized May 16, 2024 by lcjr
// vanilla
document.addEventListener('DOMContentLoaded', function() {
  var iframe = document.querySelector('.your_classname');

  if (iframe) {
    iframe.addEventListener('load', function() {
      var iframeDoc = iframe.contentDocument || iframe.contentWindow.document;
      iframeDoc.body.style.marginTop = '83px';
    });
  }
});


// jQuery
var iframe = $('.your_classname').find('iframe');

iframe.on('load', function() {
    $(this).contents().find('body').css('margin-top', '83px');
});

 

lazacode.org - Malaysia's programming knowledge sharing platform, where everyone can share their finding as reference to others.
...