2012-01-04 12 views
5

これは私が将来何百人もの人々を支援すると確信している質問です。しかし、このスクリプトはjQueryの能力を少し超えています。 jQueryの基本的なスキルがありますが、これを実行することはできません。jquery.ba-postmessageを使用したiframeの動的高さのクロスドメイン

基本的には、メインのウェブサイトに座るためにiFrame(別のドメインにホストされています)が必要です。私はiFrameを使いたくないのですが、私の状況では選択肢がありません!私はiframe内のボディの高さにリサイズするためにiFrameが必要です。

現在、Ben Alman jQuery postMessage pluginを使用していますが、これは可能なようです。しかし、現在の例では、iFrameのサイズを変更するトグルを可能にする不必要なコードがあります。

私はこのトグルは必要ありません。必要なのはiFrameが正しいiframeコンテンツボディの高さにサイズ変更することだけです。 iframe内でボディの高さが変化すると調整するためにiframeの高さが必要です。私はこれを入れているhttp://myiframecontent.com/

<script src="js/jquery.ba-postmessage.js" type="text/javascript"></script> 
<script type="text/javascript"> 

    $(function(){ 
     // Get the parent page URL as it was passed in, for browsers that don't support 
     // window.postMessage (this URL could be hard-coded). 
     var parent_url = decodeURIComponent(document.location.hash.replace(/^#/, '')), 
     link; 

     // The first param is serialized using $.param (if not a string) and passed to the 
     // parent window. If window.postMessage exists, the param is passed using that, 
     // otherwise it is passed in the location hash (that's why parent_url is required). 
     // The second param is the targetOrigin. 
     function setHeight() { 
     $.postMessage({ if_height: $('body').outerHeight(true) }, parent_url, parent); 
     }; 

     // Now that the DOM has been set up (and the height should be set) invoke setHeight. 
     setHeight(); 

     // And for good measure, let's listen for a toggle_content message from the parent. 
     $.receiveMessage(function(e){ 
     if (e.data === 'toggle_content') { 
      link.triggerHandler('click'); 
     } 
     }, 'http://mywebsite.com'); 
    }); 

</script> 

:私はiframeのコンテンツファイルでこれを入れている

これは私がこれまでに見つかったものです

...
これは、サーバー上にあります私のウェブサイト

これはサーバー上にあります:http://mywebsite.com/

<script src="js/jquery.ba-postmessage.js" type="text/javascript"></script> 
<script type="text/javascript"> 
    $(function(){ 

     // Keep track of the iframe height. 
     var if_height, 

     // Pass the parent page URL into the Iframe in a meaningful way (this URL could be 
     // passed via query string or hard coded into the child page, it depends on your needs). 

     src = 'http://myiframecontent.com/#' + encodeURIComponent(document.location.href), 

     // Append the Iframe into the DOM. 
     iframe = $('<iframe " src="' + src + '" width="1060" height="1000" scrolling="no" frameborder="0"><\/iframe>') 
      .appendTo('#iframe'); 

     // Setup a callback to handle the dispatched MessageEvent event. In cases where 
     // window.postMessage is supported, the passed event will have .data, .origin and 
     // .source properties. Otherwise, this will only have the .data property. 
     $.receiveMessage(function(e){ 

     // Get the height from the passsed data. 
     var h = Number(e.data.replace(/.*if_height=(\d+)(?:&|$)/, '$1')); 

     if (!isNaN(h) && h > 0 && h !== if_height) { 
     // Height has changed, update the iframe. 
      iframe.height(if_height = h); 
     } 

     // An optional origin URL (Ignored where window.postMessage is unsupported). 
     }, 'http://myiframecontent.com/'); 

     // And for good measure, let's send a toggle_content message to the child. 
     $('<a href="#">Show/hide Iframe content<\/a>') 
      .appendTo('#nav') 
      .click(function(){ 
      $.postMessage('toggle_content', src, iframe.get(0).contentWindow); 
      return false; 
     }); 

    }); 
</script> 

<div id="iframe" class="clear"></div> 



現在、上記は現在の幅1060pxを返すが、私のiframe内の体の高さに私のiframeの高さを変更されていませんか?

また、ナビゲーション区域の#navで自分のサイトにトグルボタンが追加されています。私が必要とするのは高さです。

ネット上で実際のサンプルを見つけることができないので、これについての助けがあれば素晴らしいですね、ありがとう!

これはba-postmessage.jsスクリプトです。

答えて

0

リンクを取り除きたい場合は、setInterval()で定期的に増加するiframeコンテンツを使用した実例があります。

これは、メインのウェブサイト/サーバーAに行く必要があります:http://mywebsite.com/

<!DOCTYPE html> 
<html> 
<head> 
<meta charset="UTF-8"> 
<title>Container on domain A (http://mywebsite.com/)</title> 
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> 
<script type="text/javascript" src="https://raw.github.com/cowboy/jquery-postmessage/master/jquery.ba-postmessage.js"></script> 
<script type="text/javascript"> 
    $(function(){ 

     // Update this constant to match your IFrame (contents) URL (no '#' here please) 
     var if_url = 'http://myiframecontent.com/'; 

     // Keep track of the iframe height. 
     var if_height; 

     // Pass the parent page URL into the Iframe in a meaningful way (this URL could be 
     // passed via query string or hard coded into the child page, it depends on your needs). 
     var src = if_url + '#' + encodeURIComponent(document.location.href); 

     // Append the Iframe into the DOM. 
     var iframe = $('<iframe " src="' + src + '" width="200" height="100" scrolling="no" frameborder="0"><\/iframe>') 
      .appendTo('#iframe'); 

     // Setup a callback to handle the dispatched MessageEvent event. In cases where 
     // window.postMessage is supported, the passed event will have .data, .origin and 
     // .source properties. Otherwise, this will only have the .data property. 
     $.receiveMessage(
      function(e){ 
       // Get the height from the passsed data. 
       var h = Number(e.data.replace(/.*if_height=(\d+)(?:&|$)/, '$1')); 

       if (!isNaN(h) && h > 0 && h !== if_height) { 
        // Height has changed, update the iframe. 
        iframe.height(if_height = h); 
        // Some user feedback if we want to... 
        $("#ticker").text("has been informed that IFrame contents height is now " + h + " and (re)acted accordingly."); 
       } 
      }, 
      // An optional origin URL (Ignored where window.postMessage is unsupported). 
      if_url 
     ); 

    }); 
</script> 
</head> 
<body> 
<p>Container <span id="ticker"></span></p> 
<div id="iframe"></div> 
<p>Container</p> 
</body> 
</html> 

そして、これは "IFRAME" サイト/サーバBに行く必要があります。http://myiframecontent.com/

<!DOCTYPE html> 
<html> 
<head> 
<meta charset="UTF-8"> 
<title>Contents on domain B (http://myiframecontent.com/)</title> 
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> 
<script type="text/javascript" src="https://raw.github.com/cowboy/jquery-postmessage/master/jquery.ba-postmessage.js"></script> 
<script type="text/javascript"> 
$(function(){ 
    // Get the parent page URL as it was passed in, for browsers that don't support 
    // window.postMessage (this URL could be hard-coded). 
    var parent_url = decodeURIComponent(document.location.hash.replace(/^#/, '')), 
    link; 

    // The first param is serialized using $.param (if not a string) and passed to the 
    // parent window. If window.postMessage exists, the param is passed using that, 
    // otherwise it is passed in the location hash (that's why parent_url is required). 
    // The second param is the targetOrigin. 
    function setHeight() { 
    $.postMessage({ if_height: $('body').outerHeight(true) }, parent_url, parent); 
    }; 

    // Let's increase height, and inform the parent document of new height, every 3 second 
    var i = 1; 
    function increaseHeight() { 
     $("#iframecontents").append("<p>Increase #" + i + "</p>"); 
     i = i + 1; 
     // Now that the DOM has been set up (and the height should be set) invoke setHeight. 
     setHeight(); 
    }; 
    var timer = window.setInterval(increaseHeight, 3000); 
}); 
</script> 
</head> 
<body> 
<p>iframe starts here</p> 
<div id="iframecontents"></div> 
<p>iframe ends here</p> 
</body> 
</html> 

はあなたの実数であることに注意してくださいIFrame(コンテンツ)ページでは、もちろんフックするには何らかのイベントが必要で、setHeight()に電話してください。

関連する問題