0

I want to inject a custom JS File within an IFrame (which is rendered via a Plugin)..

The below code in my Themes -> functions.php, injects my External JS File into the MAIN Document :

add_action('init', 'ajaxcomments_load_js', 10);
function ajaxcomments_load_js(){
        wp_enqueue_script('ajaxcomments', get_stylesheet_directory_uri().'/js/ajaxCode.js');
}

However, in Wordpress how can I inject JS File into a specific IFrame ?

Any help would be great. Thanks !

Gauri Padbidri
  • 371
  • 4
  • 15
  • I think you can only do this if the iframe is within the same domain as the originating page. **Edit:** More information here http://stackoverflow.com/questions/16194398/inject-a-javascript-function-into-an-iframe – Ding Jan 27 '15 at 22:23

1 Answers1

0

You won't be able to inject your JavaScript into an iframe if the iframe is not pointing to the domain that the page is served from. Otherwise this is considered a security risk and any modern browser will specifically block it. (Imagine if evilsite.com loaded yourbank.com in an iframe and injected JavaScript to force a bank transfer, for example). This is just not allowed.

Otherwise - if this is a domain you control you might consider this approach

Max Worg
  • 2,932
  • 2
  • 20
  • 35