title: How to call a jQuery DataTable object in an iframe?
body: <p>I have successfully initialized a jQuery DataTable in an iframe, however if I try to run a function in the iframe on the same domain it reinitializes the DataTable instead of using the existing one. How can I fix this?</p>
  
  <p>My code in the JS file of the Iframe:</p>
  
  <pre><code>$(document).ready(function(){
      listTable.initDataTables();
  });
  
  var listTable = {
      initDataTables: function() {
          $.each($('.dataTable'), function(){
              var jqTable = $(this);
  
              // Define the columns
              var columns = [];
              var columnHeadings = jqTable.find('thead &gt; tr:eq(1) &gt; th');
              $.each(columnHeadings, function(){
                  var code = $(this).attr('data-code');
                  var column = { data: code, defaultContent: '' };
                  columns.push(column);
              });
  
              // Initiate the table
              var dataTable = jqTable.DataTable({
                  'ajax': {
                      'url': '/get-data',
                      'type' : 'GET'
                  },
                  'columns': columns,
                  'responsive': true,
                  'scroller': false,
                  'select': true,
                  'bLengthChange': false,
                  'bFilter': true,
                  'bInfo': true,
              });
          });
      }
  };
  </code></pre>
  
  <p>When I run the following code in a window without an iframe, all works well:</p>
  
  <pre><code>$('table').DataTable().ajax.url();
  // returns '/get-data'
  </code></pre>
  
  <p>But when I run the following code from a parent window that contains an iframe, NULL gets returned:</p>
  
  <pre><code>$('iframe').contents().find('table').DataTable().ajax.url()
  // returns NULL
  </code></pre>
  
  <p>I also notice that when I run the code from a parent window, navigation bars get added a second time. So it looks like the DataTable() function is creating a new instance on the same table.</p>