I want to call javascript function (on click()) which is inside Iframe
<a onclick="abc();" href=# >Call Function which is inside Iframe </a>
<iframe id=myFrame>
<script>
function abc(){
alert("I am inside Iframe");
}
</script>
</iframe>
It will be better if there is solution in JQuery.
Thank you
document. getElementById("resultFrame") will get the iframe in your code, and contentWindow will get the window object in the iframe. Once you have the child window, you can refer to javascript in that context.
Iframes Bring Security Risks. If you create an iframe, your site becomes vulnerable to cross-site attacks. You may get a submittable malicious web form, phishing your users' personal data. A malicious user can run a plug-in.
To get the element in an iframe, first we need access the <iframe> element inside the JavaScript using the document. getElementById() method by passing iframe id as an argument. const iframe = document.
You can do that this way:
<a onclick="$('#myFrame')[0].contentWindow.abc();">Call function which is inside of Iframe</a>
Basically, I get a reference to the Iframe. contentWindow is the global (window) object for the iframe, and all global functions in a document are methods of window.
As a note, you can only do this if both pages, the one containing the iframe and the one in the iframe come from the same domain.
Use this to access your frame function:
document.getElementById('myFrame').contentWindow.abc()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With