2012-05-03 29 views
0

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

<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> 

そしてこのここ

は、これまでのところ、私は親ページで実行したいのiframeからのコードです親ページにロードされます:

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

答えて

1

iFrameでは、呼び出す関数をe windowオブジェクトであるため、グローバルにアクセスできます。

メインページのトップレベルのHTMLページから、iFrameDomElement.contentWindow.yourFunctionToCall()を実行できます。

EDIT:だから

、さらにそれを打破するために、あなたはあなたのコードで起こって多くの問題を抱えています。まず、intadとは何ですか? jqueryプロトタイプのメソッドですか?そうでない場合、あなたは何か間違っている。基本的に何をする必要があるのですか?

メインページでは、これからparent.htmlを呼び出します。あなたはこのような何か必要

:私はこのことができます願ってい

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

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

:あなたのiframe内に

// 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; 
}); 

を、あなたはこのようなものが必要。さらなる助けが必要な場合は、作業中の(またはほとんど働いている)コードをjsfiddleに入れて、より良い方法でそれを見ることができます。

+0

iframeからトップレベルのページにコールする必要がある場合は、iframe内で 'top'グローバル変数を使用してメインページのウィンドウオブジェクトにアクセスできます。 – sic1

+0

これはiframeから正確にどうやって行うのですか?あなたは私に作業コードを教えてください? – Patriotec

+0

アップデートを確認してください。これはiframeから親ページに連絡する方法です。 – sic1

関連する問題