christiansalazarh

comunicar iframe con ventana maestra

Oct 20th, 2013
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.33 KB | None | 0 0
  1.  
  2.  
  3. //// inicio de archivo: protected/views/algo/_subvista1.php ////
  4. <h1>Una subvista que se comunica con su vista maestra</h1>
  5. <button id='boton-dentro-de-iframe'>Click Me</button>
  6. <?php
  7. Yii::app()->getClientScript()->registerScript("algun_id",
  8. "
  9.  $('#boton-dentro-de-iframe').click(function(){
  10.    // dispara un evento a la ventana dueña del iframe:
  11.    var nombre_evento = 'boton-presionado';
  12.    parent.postMessage(nombre_evento,'*');
  13.  });
  14. ",CClientScript::POS_LOAD);
  15. //// fin de archivo ////
  16.  
  17.  
  18.  
  19. //// inicio de archivo: protected/views/algo/vistamaestra.php ////
  20. <?php
  21.    $subvista1_url = CHtml::normalizeUrl(array('subvista1'));
  22. ?>
  23. <h1>Una vista maestra que incluye a otras mediante IFRAME</h1>
  24. <iframe id='iframe1' src='{$subvista1_url}'
  25.    style='width: 800px; height: 350px;'></iframe>
  26. Yii::app()->getClientScript()->registerScript("algun_id",
  27. "
  28.     window.addEventListener('message',function(e){
  29.         var nombre_evento = e.data;
  30.                 alert('evento enviado por iframe: '+nombre_evento);
  31.     });
  32.  
  33.        // si se quiere reiniciar la vista1 se invoca a:
  34.        var _reiniciar_vista1 = function(){
  35.             document.getElementById('iframe1')
  36.               .contentDocument.location.reload(true);          
  37.        }
  38.        // ejemplo: _reiniciar_vista1();
  39. ",CClientScript::POS_LOAD);
  40. //// fin de archivo ////
Advertisement
Add Comment
Please, Sign In to add comment