Attaching a function to the parent window from an iframe and getting proper scope
        Posted  
        
            by Ronald 
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Ronald 
        
        
        
        Published on 2010-04-11T20:52:03Z
        Indexed on 
            2010/04/11
            21:03 UTC
        
        
        Read the original article
        Hit count: 258
        
I working on adding file uploading to my web application. I'm using an iframe to post my upload. When my php script processes the upload it writes some JavaScript to the iframe. This JavaScript is attempting to attach a function to the parent, this works, but when this function actually gets called it doesn't have the correct scope. Here is the code I'm attaching to the parent window:
parent.window.showPreview = function(coords)
{
    if (parseInt(coords.w) > 0)
    {
        var rx = 200 / coords.w;
        var ry = 250 / coords.h;
        $('#preview').css({
            width: Math.round(rx * 400) + 'px',
            height: Math.round(ry * 533) + 'px',
            marginLeft: '-' + Math.round(rx * coords.x) + 'px',
            marginTop: '-' + Math.round(ry * coords.y) + 'px'
        });
    }
}
When this function gets executed I get an error that says $ is not defined. I've tried adding changing the JQuery call to parent.$('#preview').css..., but then it says that parent is undefined. Any ideas?
© Stack Overflow or respective owner