View on
MetaCPAN is shutting down
For details read Perl NOC. After June 25th this page will redirect to
Marcus Ramberg > HTML-Prototype-1.35 > HTML::Prototype



Annotate this POD (9)


New  4
Open  1
View/Report Bugs
Module Version: 1.35   Source   Latest Release: HTML-Prototype-1.48


HTML::Prototype - Generate HTML and Javascript for the Prototype library


    use HTML::Prototype;

    my $prototype = HTML::Prototype->new;
    print $prototype->auto_complete_field(...);
    print $prototype->auto_complete_result(...);
    print $prototype->auto_complete_stylesheet(...);
    print $prototype->content_tag(...);
    print $prototype->define_javascript_functions;
    print $prototype->draggable_element(...);
    print $prototype->drop_receiving_element(...);
    print $prototype->evaluate_remote_response(...);
    print $prototype->form_remote_tag(...);
    print $prototype->javascript_tag(...);
    print $prototype->link_to_function(...);
    print $prototype->link_to_remote(...);
    print $prototype->observe_field(...);
    print $prototype->observe_form(...);
    print $prototype->periodically_call_remote(...);
    print $prototype->sortable_element(...);
    print $prototype->submit_to_remote(...);
    print $prototype->tag(...);
    print $ptototype->update_element_function(...);
    print $prototype->visual_effect(...);


The module contains some code generators for Prototype, the famous JavaScript OO library and the script.aculous extensions.

The Prototype library ( is designed to make AJAX easy. Catalyst::Plugin::Prototype makes it easy to connect to the Prototype library.

This is mostly a port of the Ruby on Rails helper tags for JavaScript for use in Catalyst.


$prototype->auto_complete_field( $field_id, \%options )

Adds Ajax autocomplete functionality to the text input field with the DOM ID specified by field_id.

This function expects that the called action returns a HTML <ul> list, or nothing if no entries should be displayed for autocompletion.

Required options are:

url: Specifies the URL to be used in the AJAX call.

Addtional options are:

update: Specifies the DOM ID of the element whose innerHTML should be updated with the autocomplete entries returned by the Ajax request. Defaults to field_id + '_auto_complete'.

with: A Javascript expression specifying the parameters for the XMLHttpRequest. This defaults to 'value', which in the evaluated context refers to the new field value.

indicator: Specifies the DOM ID of an elment which will be displayed while autocomplete is running.


Returns a list, to communcate with the Autocompleter.

Here's an example for Catalyst:

    sub autocomplete : Global {
        my ( $self, $c ) = @_;
        my @items = qw/foo bar baz/;
        $c->res->body( $c->prototype->auto_complete_result(\@items) );

Returns the auto_complete stylesheet.

$prototype->content_tag( $name, $content, \%html_options )

Returns a block with opening tag, content, and ending tag. Useful for autogenerating tags like <a href=""Catalyst Homepage</a>>. The first parameter is the tag name, i.e. 'a' or 'img'.


Returns the library of JavaScript functions and objects, in a script block.

Notes for Catalyst users:

You can use script/ Prototype to generate a static JavaScript file which then can be included via remote script tag.

$prototype->draggable_element( $element_id, \%options )

Makes the element with the DOM ID specified by element_id draggable.


    $prototype->draggable_element( 'my_image', { revert => 'true' } );

The available options are:


Default: none. Sets whether the element should only be draggable by an embedded handle. The value is a string referencing a CSS class. The first child/grandchild/etc. element found within the element that has this CSS class will be used as the handle.


Default: false. If set to true, the element returns to its original position when the drags ends.


Default: none. If set to 'horizontal' or 'vertical' the drag will be constrained to take place only horizontally or vertically.


Javascript callback function called whenever the Draggable is moved by dragging. It should be a string whose contents is a valid JavaScript function definition. The called function gets the Draggable instance as its parameter. It might look something like this:

    'function (element) { // do something with dragged element }'

See for more documentation.

$prototype->drop_receiving_element( $element_id, \%options )

Makes the element with the DOM ID specified by element_id receive dropped draggable elements (created by draggable_element).

And make an AJAX call.

By default, the action called gets the DOM ID of the element as parameter.

Example: $prototype->drop_receiving_element( 'my_cart', { url => '' } );

Required options are:


The URL for the AJAX call.

Additional options are:


Default: none. Set accept to a string or an array of strings describing CSS classes. The Droppable will only accept Draggables that have one or more of these CSS classes.


Default: none. The droppable will only accept the Draggable if the Draggable is contained in the given elements (or element ids). Can be a single element or an array of elements. This is option is used by Sortables to control Drag-and-Drop between Sortables.


Default: none. If set to 'horizontal' or 'vertical' the droppable will only react to a Draggable if it overlaps by more than 50% in the given direction. Used by Sortables.

Additionally, the following JavaScript callback functions can be used in the option parameter:


Javascript function called whenever a Draggable is moved over the Droppable and the Droppable is affected (would accept it). The callback gets three parameters: the Draggable, the Droppable element, and the percentage of overlapping as defined by the overlap option. Used by Sortables. The function might look something like this:

    'function (draggable, droppable, pcnt) { // do something }'

See for more documentation.


Returns 'eval(request.responseText)' which is the Javascript function that form_remote_tag can call in :complete to evaluate a multiple update return document using update_element_function calls.


Returns a form tag that will submit in the background using XMLHttpRequest, instead of the regular reloading POST arrangement.

Even though it is using JavaScript to serialize the form elements, the form submission will work just like a regular submission as viewed by the receiving side.

The options for specifying the target with url and defining callbacks are the same as link_to_remote.

$prototype->javascript_tag( $content, \%html_options )

Returns a javascript block with opening tag, content and ending tag.

$prototype->link_to_function( $name, $function, \%html_options )

Returns a link that will trigger a JavaScript function using the onClick handler and return false after the fact.


    $prototype->link_to_function( "Greeting", "alert('Hello world!') )
    $prototype->link_to_function( '<img src="really.png"/>', 'do_delete()' )
$prototype->link_to_remote( $content, \%options, \%html_options )

Returns a link to a remote action defined by options url that's called in the background using XMLHttpRequest.

The result of that request can then be inserted into a DOM object whose id can be specified with options->{update}.


    $prototype->link_to_remote( 'Delete', {
        update => 'posts',
        url    => 'http://localhost/posts/'
    } )

    $prototype->link_to_remote( '<img src="refresh.png"/>', {
        update => 'emails',
        url    => 'http://localhost/refresh/'
    } )

By default, these remote requests are processed asynchronously, during which various callbacks can be triggered (e.g. for progress indicators and the like).


    $prototype->link_to_remote( 'count', {
        url => 'http://localhost/count/',
        complete => 'doStuff(request)'
    } )

The callbacks that may be specified are:

loading: Called when the remote document is being loaded with data by the browser.

loaded: Called when the browser has finished loading the remote document.

interactive: Called when the user can interact with the remote document, even though it has not finished loading.

complete: Called when the XMLHttpRequest is complete.

If you do need synchronous processing (this will block the browser while the request is happening), you can specify $options->{type} = 'synchronous'.

You can customize further browser side call logic by passing in Javascript code snippets via some optional parameters. In their order of use these are:

confirm: Adds confirmation dialog.

condition: Perform remote request conditionally by this expression. Use this to describe browser-side conditions when request should not be initiated.

before: Called before request is initiated.

after: Called immediately after request was initiated and before loading.

$prototype->observe_field( $id, \%options)

Observes the field with the DOM ID specified by $id and makes an Ajax when its contents have changed.

Required options are:

frequency: The frequency (in seconds) at which changes to this field will be detected.

url: url to be called when field content has changed.

Additional options are:

update: Specifies the DOM ID of the element whose innerHTML should be updated with the XMLHttpRequest response text.

with: A JavaScript expression specifying the parameters for the XMLHttpRequest. This defaults to value, which in the evaluated context refers to the new field value.

Additionally, you may specify any of the options documented in link_to_remote.

Example TT2 template in Catalyst:

    [% c.prototype.define_javascript_functions %]
    <h1>[% page.title %]</h1>
    <div id="view"></div>
    <textarea id="editor" rows="24" cols="80">[% page.body %]</textarea>
    [% url = base _ 'edit/' _ page.title %]
    [% c.prototype.observe_field( 'editor', {
        url    => url,
        with   => "'body='+value",
        update => 'view'
    } ) %]
$prototype->observe_form( $id, \%options )

Like observe_field, but operates on an entire form identified by the DOM ID $id.

Options are the same as observe_field, except the default value of the with option evaluates to the serialized (request string) value of the form.

$prototype->periodically_call_remote( \%options )

Periodically calls the specified url $options->{url} every $options->{frequency} seconds (default is 10).

Usually used to update a specified div $options->{update} with the results of the remote call.

The options for specifying the target with url and defining callbacks is the same as link_to_remote.

$prototype->sortable_element( $element_id, \%options )

Makes the element with the DOM ID specified by +element_id+ sortable by drag-and-drop and make an Ajax call whenever the sort order has changed. By default, the action called gets the serialized sortable element as parameters.

Example: $ptototype->sortable_element( 'my_list', { url => '' } );

In the example, the action gets a "my_list" array parameter containing the values of the ids of elements the sortable consists of, in the current order.

You can change the behaviour with various options, see for more documentation.

$prototype->submit_to_remote( $name, $value, \%options )

Returns a button input tag that will submit a form using XMLHttpRequest in the background instead of a typical reloading via POST.

options argument is the same as in form_remote_tag

$prototype->tag( $name, \%options, $starttag );

Returns a opening tag.

$prototype->update_element_function( $element_id, \%options, \&code )

Returns a Javascript function (or expression) that'll update a DOM element according to the options passed.

content: The content to use for updating. Can be left out if using block, see example.

action: Valid options are update (assumed by default), :empty, :remove

position: If the :action is :update, you can optionally specify one of the following positions: :before, :top, :bottom, :after.

Example: $prototype->javascript_tag( $prototype->update_element_function( 'products', { position => 'bottom', content => '<p>New product!</p>' ) );

This method can also be used in combination with remote method call where the result is evaluated afterwards to cause multiple updates on a page.

Example: # View $prototype->form_remote_tag( { url => { "" }, complete => $prototype->evaluate_remote_response } );

    # Returning view
    $prototype->update_element_function( 'cart', {
        action   => 'update',
        position => 'bottom', 
        content  => "<p>New Product: $product_name</p>"
    } );
    $prototype->update_element_function( 'status',
        { binding => "You've bought a new product!" } );
$prototype->visual_effect( $name, $element_id, \%js_options )

Returns a JavaScript snippet to be used on the Ajax callbacks for starting visual effects.

    $prototype->link_to_remote( 'Reload', {
        update   => 'posts',
        url      => '',
        complete => $prototype->visual_effect( 'highlight', 'posts', {
            duration => '0.5'
        } )
    } );


Catalyst::Plugin::Prototype, Catalyst.


Sebastian Riedel, Marcus Ramberg,

Built around Prototype by Sam Stephenson. Much code is ported from Ruby on Rails javascript helpers.


Drew Taylor, Leon Brocard, Andreas Marienborg


This library is free software. You can redistribute it and/or modify it under the same terms as perl itself.

syntax highlighting: