views:

41

answers:

2

I have loaded a page in Fancybox using a iframe. But when I try to perform jQuery functions within that iframe it won't work. How do I access the iframe page or content so that I can perform jQuery on that page? I tried this below but doesnt work.

jQuery(document).ready(function(){
parent.jQuery("#showcasef", jQuery.browser.msie ? parent.jQuery('body')[0] : parent.jQuery(document)).live('submit', function(e) {  
                  e.preventDefault();   
        return false;
        var pho_id  = jQuery('#pho_id').attr('value');
        var showcase =jQuery("#showcase_pho option:selected").val();
        var action = "showcasep";
        alert("here");


    }
  );                

});

+1  A: 

You will need to include jQuery again inside that iframe, or try...

parent.$('body').text('Does it work?');

Or maybe if that is too long, make a wrapper

function $(selector, context) {

     return parent.jQuery(selector, context);
}
alex
Thanks but can't get it to work. MUST still be doing something wrong. I provide a code sample above if that helps. Thx!
Pjack
@Pjack Is the `iframe` on the same domain? Also, you are returning false from the function in the 2nd line. This will stop the rest of the anonymous function from being ran.
alex
Yes it is on the same domain. Oops, I moved it there to test something, I moved it back to the end. However it still doesn't work. Thx
Pjack
Im an idiot. I figured out the problem. I have a js file with all my functions. Well I use a script that can use different header templates. The header being called on the page did not have my js file included. Once added, it worked. Duh. Thanks for the answers. Yes I just needed to include jQuery and my js file again.
Pjack
@Pjack Glad you figured it out :)
alex
A: 

If both documents depend on the same domain, you can access jQuery by prepending "parent" to your statement.

Example:

<body> 
<script>
alert(parent.$('body',document).html())
</script>
</body>

Notice the 2nd argument is set to document. This is recommended, otherwise the context will be the document of the parent window, not the document inside the iframe. But as I did not know if this kind of use is really secure, especially if you use external plugins, I would prefer the additional including.

Dr.Molle