0

通常のjavascriptでiframeを作成しました。その iframe には、JQuery とともに Jquery 関数があります。コードは、期待どおりに iframe で機能します。iframe内ではなく、親ページに機能を実行させたい。iFrame とすべてのコンテンツは同じドメインから読み込まれますが、iframe を作成するコードは任意のサイトで実行できます。たとえば、siteB、siteA がコードを実行した場合、siteA & B は siteZ の Web サイト コンテンツへの iframe を作成できます。

親ページで実行したいiframeからこれまでのコードは次のとおりです。

<script>
$(document).ready(function(){                       
    $().intad({
        title: "blahblah",
        url: "http://www.blah.com",
        timeout: 30,
        deplay: 2,
        wait: 3,
        closeable: true
    });
});
</script>

iframeからこれを試しました:

<script>
$(document).ready(function(){                       
    function intad(){
        title: "blahblah",
        url: "http://www.blah.com",
        timeout: 30,
        deplay: 2,
        wait: 3,
        closeable: true
    });
});
</script>

そして、これは親ページに読み込まれるコードから:

<script>
parent.document.getElementById("RandomIdHere").contentWindow.intads();
</script>
4

1 に答える 1

1

iFrame では、呼び出したい関数をwindowオブジェクトに配置して、グローバルにアクセスできるようにする必要があります。

そこにあると、メインのトップレベルの html ページから、次のことができますiFrameDomElement.contentWindow.yourFunctionToCall()

編集:

したがって、さらに詳しく説明すると、コードで多くの問題が発生しています。まず、何intadですか?jqueryプロトタイプのメソッドですか?そうでない場合は、何か間違ったことをしています。ここでは基本的に何をする必要があります。

あなたのメインページで、これからparent.htmlと呼びます。

次のようなものが必要です。

// this is a shorthand document ready function
$(function() {

    // we will call this from the iframe
    window.functionInParent = function() {
        // do your work here
    };

    var iframe = document.createElement('iframe');
    iframe.src = 'link/to/iframe/source';

    // if you want to talk to the iframe from this parent page,
    // use this to access the iframe's window object.
    // just make sure that the function you try to call is on the
    // window object of the iframe, just like we did with the function above.
    var iframeWindow = iframe.contentWindow;
});

iframe では、次のようなものが必要です。

$(function() {
    // your iframe's dom is ready - do stuff here

    // once you are ready, call the parent function
    top.functionInParent();
});

これが役立つことを願っています。さらに助けが必要な場合は、作業中の (またはほとんど作業中の) コードをjsfiddleに入れて、より良い方法で確認できるようにしてください。

于 2012-05-03T17:56:01.333 に答える