Access jQuery library from iframe

10,048

You could try running this from inside your iframe:

var $ = jQuery = window.parent.$;

Assuming the parent and iframe are on the same domain.

Just tested it, example: http://jsfiddle.net/qA7yE/

(That example uses different code - the child iframe calls the parent's foo() function, but the same principle applies)

Also, just to be on the safe side you may want to do:

var $ = window.parent.$, jQuery = window.parent.jQuery;
Share:
10,048
Doose
Author by

Doose

Updated on June 16, 2022

Comments

  • Doose
    Doose almost 2 years

    I have a legacy application that uses iframes. It has an iframe in the parent page, that is dynamically replaced with other pages.

    JQuery is loaded in the parent. Is there some type of plugin that will allow me to access the jquery core that is loaded in the parent from the iframe pages without including jquery (language="JavaScript" src="../javascript/jquery.js") in the multiple child (iframe) pages?

    For example, the iframe is static

    <iframe name="mainWindow" src="includes/View.asp frameborder="0" />
    

    I know there are better ways to do this, but I am stuck with this architecture at the moment. Any suggestions?

  • Doose
    Doose about 13 years
    Thanks guys will give it a go
  • Doose
    Doose about 13 years
    Cant get this to work, am kinda new to jquery. this is what I have in the iframe<script> var $ = window.parent.$, jQuery = window.parent.jQuery; $(document).ready(function() { $(".someClass").click(function () { alert('blah'); }); }); </script> <div class="someClass">foo</div> and the iframe is in a page that has the jquery core.
  • mattsven
    mattsven about 13 years
    OK, are the pages on the same domain? I would try something more simple, just to make sure it is working like window.parent.foo(), and check the console.
  • namal
    namal about 8 years
    This is not work on fancybox. How can I solve this problem?
  • mattsven
    mattsven about 8 years
    I'm not familiar with Fancybox, and how it works. You should post a separate question.