Panama Jack
Panama Jack

Reputation: 24478

Call jQuery from a Fancybox iframe?

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");


    }
  );                

});

Upvotes: 2

Views: 2180

Answers (2)

alex
alex

Reputation: 490657

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 $() {
     return parent.jQuery.call(this, arguments);
}

Upvotes: 2

Dr.Molle
Dr.Molle

Reputation: 117364

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.

Upvotes: 1

Related Questions