Rendering a module's template contents in the page body I've been digging through the Drupal docs and haven't found a solution for this:

What I'm trying to do is to create a module with a URL using menu hooks. I've got that far. When this URL is viewed, I'd like to insert some HTML and JS code in the body but I haven't managed that. I've been trying to use templates.

This is what my module looks like:

&lt;?php
  function hello_menu(){
    $items = array();

    $items['nodes'] = array(
      'title'            =&gt; t('My Nodes'),
      'page callback'    =&gt; 'helloworld_output',
      'access arguments' =&gt; array('access content'),
    );

    return $items;
  }

  function hello_theme($existing, $type, $theme, $path){
    return array(
      'timeline' =&gt; array(
        'variables' =&gt; array('content' =&gt; NULL),
        'file' =&gt; 'timeline.tpl.php',
        'path' =&gt; drupal_get_path('module', 'hello')
      )
    );
  }

  function helloworld_output() {    
    header('Content-type: text/plain; charset=UTF-8');
    header('Content-Disposition: inline');    
    return theme('timeline', array());    
  }
?&gt;


..and this is what I want rendered in the body.

&lt;script&gt;
    var tl;
    function onLoad() {
        alert('I have loaded');
    }
&lt;/script&gt;
&lt;div id="tl" class="timdef" style="height: 300px;"&gt;&lt;/div&gt;


If I place this code into my timeline.tpl.php, it doesn't get rendered in the page but before the whole content. Do I need to wrap this in PHP tags?

How can I get the onLoad() function to fire once my Drupal page is loaded. I a normal HTML page, I could use the body onload= callback.

(It looks really simple when you've worked with Drupal but for a first-timer, it's been a nightmare.)
The best way to add the JavaScript in Drupal is to have an external JS file like helloworld.js in your module/theme and include it using the function,

  drupal_add_js('jQuery(document).ready(function () { alert("Hello!"); });', 'inline');
  drupal_add_js('jQuery(document).ready(function () { alert("Hello!"); });',
    array('type' =&gt; 'inline', 'scope' =&gt; 'footer', 'weight' =&gt; 5)
   );
  drupal_add_js('http://example.com/example.js', 'external');
  drupal_add_js(array('myModule' =&gt; array('key' =&gt; 'value')), 'setting');


For more info see drupal_add_js() 


  How can I get the onLoad() function to fire once my Drupal page is loaded. I a normal HTML page, I could use the body onload= callback


jQuery(document).ready is similar to your onload function that you are trying to call in your HTML body.

UPDATE:

Add the "template" instead of file in your  hook_theme for using templates like,

 function hello_theme($existing, $type, $theme, $path){
    return array(
      'timeline' =&gt; array(
        'variables' =&gt; array('content' =&gt; NULL),
        'template' =&gt; 'timeline',
        'path' =&gt; drupal_get_path('module', 'hello')
      )
    );
  }