jQuery: The Write Less, Do More JavaScript Library

How to catch function call

View: New views
6 Messages — Rating Filter:   Alert me  

How to catch function call

by Luiz Abrahao :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Hello,

I have one page with one iframe, and there are few javascript function
on the page inside the iframe. These functions are triggered by some
flash presentations.

Basically the parent page has to 'know' when the user has requested
the next page (inside the iframe) and update its content with relevant
data related with this new page inside the iframe.

I can't change the javascript functions from the pages inside the
iframe.

Thanks
Luiz.

Re: How to catch function call

by Michael Geary-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Is the iframe loaded from the same domain as the containing page, or a
different domain?

> From: Luiz Abrahao
>
> I have one page with one iframe, and there are few javascript
> function on the page inside the iframe. These functions are
> triggered by some flash presentations.
>
> Basically the parent page has to 'know' when the user has
> requested the next page (inside the iframe) and update its
> content with relevant data related with this new page inside
> the iframe.
>
> I can't change the javascript functions from the pages inside
> the iframe.
>
> Thanks
> Luiz.


Re: How to catch function call

by Luiz Abrahao :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Michael, thanks for your time,

It's loaded form the same domain.


On Jul 3, 3:41 pm, "Michael Geary" <m...@...> wrote:

> Is the iframe loaded from the same domain as the containing page, or a
> different domain?
>
> > From: Luiz Abrahao
>
> > I have one page with one iframe, and there are few javascript
> > function on the page inside the iframe. These functions are
> > triggered by some flash presentations.
>
> > Basically the parent page has to 'know' when the user has
> > requested the next page (inside the iframe) and update its
> > content with relevant data related with this new page inside
> > the iframe.
>
> > I can't change the javascript functions from the pages inside
> > the iframe.
>
> > Thanks
> > Luiz.
>
>

Re: How to catch function call

by Luiz Abrahao :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Hi Micheael,

Thanks for your time.

Yes, it is. The pages that are loaded into the iframe are form the
same domain.

Thanks

On Jul 3, 3:41 pm, "Michael Geary" <m...@...> wrote:

> Is the iframe loaded from the same domain as the containing page, or a
> different domain?
>
> > From: Luiz Abrahao
>
> > I have one page with one iframe, and there are few javascript
> > function on the page inside the iframe. These functions are
> > triggered by some flash presentations.
>
> > Basically the parent page has to 'know' when the user has
> > requested the next page (inside the iframe) and update its
> > content with relevant data related with this new page inside
> > the iframe.
>
> > I can't change the javascript functions from the pages inside
> > the iframe.
>
> > Thanks
> > Luiz.
>
>

Re: How to catch function call

by Michael Geary-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


That's good - if it were a different domain we'd be in trouble!

I forgot to ask, but the JavaScript function you want to intercept is a
global function in the iframe, correct? Then you could use something like
this plugin (untested):

    ;(function( $ ) {
        $.fn.frameHook = function( name, hook ) {
            this.each( function() {
                var win = this.contentWindow;
                var target = win[name];
                win[name] = function() {
                    hook.apply( win, arguments );
                    return target.apply( win, arguments );
                };
            });
        };
    })( jQuery );

Given an iframe with id="myframe" and a global function inside the iframe
named 'foobar', you would do:

    $('#myframe').frameHook( 'foobar', function() {
        alert( 'about to call foobar' );
    });

To help illustrate, here's a bare metal version without the jQuery
boilerplate:

    function frameHook( iframe, name, hook ) {
        var win = iframe.contentWindow;
        var target = win[name];
        win[name] = function() {
            hook.apply( win, arguments );
            return target.apply( win, arguments );
        };
    }

    var iframe = $('#myframe')[0];
    frameHook( iframe, 'foobar', function() {
        alert( 'about to call foobar' );
    });

-Mike

> From: Luiz Abrahao
> Michael, thanks for your time,
>
> It's loaded form the same domain.
>
> > On Jul 3, 3:41 pm, "Michael Geary" <m...@...> wrote:
> > Is the iframe loaded from the same domain as the containing
> > page, or a different domain?
> >
> > > From: Luiz Abrahao
> > > I have one page with one iframe, and there are few javascript
> > > function on the page inside the iframe. These functions are
> > > triggered by some flash presentations.
> > >
> > > Basically the parent page has to 'know' when the user has
> > > requested the next page (inside the iframe) and update its content
> > > with relevant data related with this new page inside the iframe.
> > >
> > > I can't change the javascript functions from the pages inside the
> > > iframe.


Re: How to catch function call

by Luiz Abrahao :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Hi Michael,

I tried, but I've got no response. I double checked the spelling (the
iframe's id and the javascript function's name), and the function is
global.

What I did:

- I'm using jquery-1.2.6.min.js
- inside my head tag I have:

<script type="text/javascript">

;(function( $ ) {
   $.fn.frameHook = function( name, hook ) {
      this.each( function() {
         var win = this.contentWindow;
         var target = win[name];
         win[name] = function() {
            hook.apply( win, arguments );
            return target.apply( win, arguments );
         };
      });
   };
})( jQuery );

$('#course').frameHook( 'trivNextPage', function() {
   alert( 'about to call trivNextPage' );
});

</script>


but nothing happens when I trigger the trivNextPage function, if I
assign a breakpoint to the line "$
('#course').frameHook( 'trivNextPage', function()" the the firebug
(1.2.0b4) only stops at this point when the page is refreshed. but the
function never is executed.

I don't know if I'm doing something stupid.

Many Thanks
Luiz


On Jul 4, 9:18 pm, "Michael Geary" <m...@...> wrote:

> That's good - if it were a different domain we'd be in trouble!
>
> I forgot to ask, but the JavaScript function you want to intercept is a
> global function in the iframe, correct? Then you could use something like
> this plugin (untested):
>
>     ;(function( $ ) {
>         $.fn.frameHook = function( name, hook ) {
>             this.each( function() {
>                 var win = this.contentWindow;
>                 var target = win[name];
>                 win[name] = function() {
>                     hook.apply( win, arguments );
>                     return target.apply( win, arguments );
>                 };
>             });
>         };
>     })( jQuery );
>
> Given an iframe with id="myframe" and a global function inside the iframe
> named 'foobar', you would do:
>
>     $('#myframe').frameHook( 'foobar', function() {
>         alert( 'about to call foobar' );
>     });
>
> To help illustrate, here's a bare metal version without the jQuery
> boilerplate:
>
>     function frameHook( iframe, name, hook ) {
>         var win = iframe.contentWindow;
>         var target = win[name];
>         win[name] = function() {
>             hook.apply( win, arguments );
>             return target.apply( win, arguments );
>         };
>     }
>
>     var iframe = $('#myframe')[0];
>     frameHook( iframe, 'foobar', function() {
>         alert( 'about to call foobar' );
>     });
>
> -Mike
>
> > From: Luiz Abrahao
> > Michael, thanks for your time,
>
> > It's loaded form the same domain.
>
> > > On Jul 3, 3:41 pm, "Michael Geary" <m...@...> wrote:
> > > Is the iframe loaded from the same domain as the containing
> > > page, or a different domain?
>
> > > > From: Luiz Abrahao
> > > > I have one page with one iframe, and there are few javascript
> > > > function on the page inside the iframe. These functions are
> > > > triggered by some flash presentations.
>
> > > > Basically the parent page has to 'know' when the user has
> > > > requested the next page (inside the iframe) and update its content
> > > > with relevant data related with this new page inside the iframe.
>
> > > > I can't change the javascript functions from the pages inside the
> > > > iframe.
>
>
LightInTheBox - Buy quality products at wholesale price