How to insert javascript code in iframe tag?

by lindsey.homenick , in category: Third Party Scripts , 8 months ago

How to insert javascript code in iframe tag?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by giovanny.lueilwitz , 8 months ago

@lindsey.homenick 

To insert JavaScript code in an iframe tag, you can use the "srcdoc" attribute or the "onload" attribute.

  1. Using the "srcdoc" attribute:
1
<iframe srcdoc="<script>alert('Hello!')</script>"></iframe>


  1. Using the "onload" attribute:
1
<iframe src="about:blank" onload="this.contentWindow.document.body.innerHTML = '<script>alert("Hello!")</script>';"></iframe>


Please note that inserting JavaScript code in an iframe can be a security risk, so make sure to only include trusted code.

Related Threads:

How to run a javascript code over an iframe?
How to update an iframe with javascript?
How to add javascript code in Drupal 8?
How to test JavaScript code with PHPUnit?
How to use docker image tag parsing regex in javascript?
How to append elements in head tag from iframe?