serverside not executing JavaScript

serverside not executing JavaScript

fisharebestfisharebest Posts: 17Questions: 1Answers: 0
edited February 2011 in General
I have a datatable with a serverside data source.

It also uses row-details (also a serverside data source), as per this http://datatables.net/examples/server_side/row_details.html

I want to add JavaScript into both table cells and the row details. Something simple, such as

alert("hello");

Now, this works fine in Firefox. But in many other browsers (such as Chrome), it only works in the row-details. The JavaScript in the table cells is never executed.

Does datatables use the same method for inserting content for both the table cells and the detail row?

Replies

  • allanallan Posts: 63,112Questions: 1Answers: 10,395 Site admin
    The way DataTables inserts content is by simply writing to the innerHTML property of the table cell in question. As a result of this, and the fact that browsers will not run Javascript which is inserted in such a manner, it means that simply adding a script element with Javascript inside it will not work. If you want to do this, then you could use a string matching regex to pull out the script tag and insert it into the document in a DOM created script tag. A google search for 'innerHTML script tag' should help.

    Allan
  • fisharebestfisharebest Posts: 17Questions: 1Answers: 0
    Thanks.

    I guess my data source can provide the JS in a hidden column, to make it easier for the client code to pull out.
  • fisharebestfisharebest Posts: 17Questions: 1Answers: 0
    If it helps anyone else, the following callback function fixes the problem for me.

    [code]
    "fnDrawCallback": function() {
    // Our JSON responses include JavaScript as well as HTML. This does not get
    // executed (except for some versions of Firefox?). So, extract it, and add
    // it to its own DOM element
    jQuery('#table script').each(function() {
    var script=document.createElement('script');
    script.type='text/javascript';
    jQuery(script).text(jQuery(this).text());
    document.body.appendChild(script);
    }).remove();
    }
    [/code]
This discussion has been closed.