Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to deal with iframe with DOMContent when user clicked something?

I have an website with content, with div class card2 which has white background and some buttons (button1, button2, button3)

I have dashboard, where I load an website content using iframe (full html)

So, when user opens his dashboard, I apply the code bellow which will remove a white background from style and buttons will be on the background without white color

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel


<script>

  window.addEventListener('DOMContentLoaded', (event) => {
    console.log('DOM fully loaded and parsed');
    document.getElementById("biolink_preview_iframe").load = function(){}
    let iframeName = document.getElementById("biolink_preview_iframe");
    let iframeContent = iframeName.contentDocument;
    iframeContent.body.innerHTML = iframeContent.body.innerHTML + "<style>.card2{background-color: unset !important;box-shadow: none !important}</style>";

  });

</script>

When user change the order of a buttons in dashboard, it reloads and iframe, and my code above is not working anymore, white background is visible again.

Can you suggest me please, why this happening?

I tried this code

<script>

  window.addEventListener('DOMContentLoaded', (event) => {
    console.log('DOM fully loaded and parsed');
    document.getElementById("biolink_preview_iframe").load = function(){}
    let iframeName = document.getElementById("biolink_preview_iframe");
    let iframeContent = iframeName.contentDocument;
    iframeContent.body.innerHTML = iframeContent.body.innerHTML + "<style>.card2{background-color: unset !important;box-shadow: none !important}</style>";

  });

</script>

>Solution :

Typos.

You need either

document.getElementById("biolink_preview_iframe").onload = function

OR

document.getElementById("biolink_preview_iframe").addEventListener('load', function

and you have a {} after function. You likely meant

window.addEventListener('DOMContentLoaded', (event) => {
  console.log('DOM fully loaded and parsed');
  document.getElementById("biolink_preview_iframe").addEventListener('load', function() {
    let iframeContent = this.contentDocument;
    iframeContent.body.innerHTML += "<style>.card2{background-color: unset !important;box-shadow: none !important}</style>";
  });
});
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading