How to run iFrame script from parent webpage?

So let's say that I have a webpage, webpage B. Webpage B has the function "test ()". Now let me say that I have another webpage, webpage A. Webpage A has an iFrame with the source of webpage B. How can I execute webpage B "test ()" from webpage A?

+3
source share
1 answer

You cannot directly control javascript pages in iframes with any code on the parent page.

If you have the ability to change both pages, you can change "page B" to run javascript based on the parameters in this query string.

' A' iframe src - :

http://mydomain.com/pageb.html?func=myfunc¶m=foo¶m2=bar

'page B' javascript :

function queryToLookup(query) {
    var lookup= {};
    var params= query.slice(1).split(/[&;]/);
    for (var i= 0; i<params.length; i++) {
        var ix= params[i].indexOf('=');
        if (ix!==-1) {
            var name= decodeURIComponent(params[i].slice(0, ix));
            var value= decodeURIComponent(params[i].slice(ix+1));
            if (!(name in lookup))
                lookup[name]= [];
            lookup[name].push(value);
        }
    }
    return lookup;
}

function myTestFunc(p1, p2) {
    alert('param 1 is ' + p1 + ' and p2 is ' + p2);
}

jQuery(document).ready(function() {
    var params = queryToLookup(location.search);
    switch(lookup['func'])
    {
        case 'myfunc': myTestFunc(params['param1'], params['param2']);
        case 'otherfunc': myTestFunc('someval', params['param1']);
        default: alert('No function requested');
    }
});

, , , . , "A", ( ) A, top.location = 'pagea.html?message=foo' (, ).

+1

All Articles