Let's assume, for instance, an xHTML document as below:
<html>
<head>
</head>
<body>
<div id="wrapper">
<div id="sidebar"></div>
<div id="content"></div>
</div>
</body>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript" src="./application.js"></script>
</html>
We'll be performing a GET AJAX request to a PHP script that will query a database and return a response to populate a TABLE
element which will be formatted as a sortable grid via JavaScript.
Additionally, we want all of our JavaScript in one file that can easily be included as displayed above.
The options:
Have the PHP script return the
TABLE
element populated with all of the data. Use jQuery'slive()
function to actively watch for newTABLE
elements in the DOM and apply the sortable grid functionality when it discovers them. This method allows us to manipulate the content retruned from the AJAX request in PHP and returns a completed semantic table to the parent document.Insert the
TABLE
element into the script listed above, but leaving it's content empty. jQuery would immediately apply the sortable grid functionality once the document is ready, although no content would exist yet. The GET request would then return a JSON response that would populate the database.
Any other opinions / best practices / use cases you can share for this sort of thing? I am only discussing a TABLE
here but we have been discussing the issue with a number of things: lightboxes, drop-down menus, etc.
Currently, we are not using jQuery's live()
function nor do the DOM elements exist within the parent document. So, we are having to place small blocks of Javascript at the top of each of our individual Views. Although sloppy, this has allowed us to only load functionality when it is needed, but it is obviously difficult to maintain.