0

I hava page where a user can create multiple iframes.

User clicks on link to create new iframe:

parent page:

 echo "<div id='iframes_wrapper'>";
 $q = mysql_query("SELECT * FROM chat_iframe WHERE init_uid='$_SESSION[uid]'");
 while ($row = mysql_fetch_assoc($q)) {
  $target_uid = $row['target_uid'];  
  $iframeid = $row['id'];
  echo "<iframe id='iframe_$iframeid' src='includes/chat_iframe.php?iframeid=$iframeid' class='chat_iframe' scrolling='no' frameborder='0' data-target_uid='$target_uid'></iframe>";
 }
 echo "</div>";

$(document).ready(function() { 
  $("#window").delegate(".open_iframe","click", function() { 
    var target_uid = $(this).attr("data-uid"); 
    $.post("processforms/process_chat.php", { target_uid:target_uid, open_iframe_submit:0 }, function() {
     $("#iframes_wrapper").load('chat.php?timer='+new Date().getTime() + " #iframes_wrapper", function() {
         $("iframe").each(function() {
           var this_iframeid = $(this).attr("id");
           console.log(this_iframeid);
           $("#iframe_"+this_iframeid).contents().find("a").on('click', function(event) { alert('test'); });
         });
        return false;
     });
    });
  });
  return false;
});  

iframe.php

 echo "$target_displayname <a href='javascript:void(0);' data-iframeid='$iframeid' class='close_chat_iframe'><i class='fa fa-remove' style='float:right;'></i>";

The problem I'm having is that when the user clicks on the link in the iframe, nothing happens.

Bijan
  • 7,737
  • 18
  • 89
  • 149
DobotJr
  • 3,929
  • 9
  • 36
  • 49
  • Use jQuery's method for data attributes: `$.data()` – Sterling Archer Nov 13 '14 at 19:22
  • @SterlingArcher ok I'll check it out. – DobotJr Nov 13 '14 at 19:24
  • Please, [don't use `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php), They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://us1.php.net/pdo) or [MySQLi](http://us1.php.net/mysqli). You will also want to [Prevent SQL Injection!](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) – Jay Blanchard Nov 13 '14 at 19:25
  • You should use the onload event of the iframe to set handler once iframe is loaded, not before – A. Wolff Nov 13 '14 at 19:25
  • @A.Wolff example would be nice. Thanks in advance! – DobotJr Nov 13 '14 at 19:26

1 Answers1

1

You should try instead of $("iframe").each(function() {...});:

$("iframe", this).on('load', function () {...});
A. Wolff
  • 74,033
  • 9
  • 94
  • 155