How to insert javascript code in iframe tag?

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

How to insert javascript code in iframe tag?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by giovanny.lueilwitz , 3 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.