Mojolicious::Plugin::JSLoader - move js loading to the end of the document
version 0.02
In your startup:
startup
sub startup { my $self = shift; # do some Mojolicious stuff $self->plugin( 'JSLoader' ); # more Mojolicious stuff }
In your template:
<% js_load('js_file.js') %>
This plugin adds a helper method to your web application:
This method requires at least one parameter: The path to the JavaScript file to load. An optional second parameter is the configuration. You can switch off the base for this JavaScript file this way:
# <script type="text/javascript" src="$base/js_file.js"></script> <% js_load('js_file.js') %> # <script type="text/javascript" src="http://domain/js_file.js"></script> <% js_load('http://domain/js_file.js', {no_base => 1});
There are several config options for js_load:
js_load
no_base
Do not use the base url configured on startup when no_base is set to a true value.
# <script type="text/javascript" src="http://domain/js_file.js"></script> <% js_load('http://domain/js_file.js', {no_base => 1});
no_file
If set to a true value, you have to pass pure JavaScript
# <script type="text/javascript">alert('test');</script> <% js_load("alert('test')", {no_file => 1});
inplace
Do not load the javascript at the end of the page, but where js_load is called.
# <script type="text/javascript" src="http://domain/js_file.js"></script> <%= js_load('http://domain/js_file.js', {no_base => 1, inplace => 1});
browser
Load the javascript when a specific browser is used.
# Load the javascript when Internet Explorer 8 is used # <script type="text/javascript" src="http://domain/js_file.js"></script> <%= js_load('http://domain/js_file.js', {inplace => 1, browser => { "Internet Explorer" => 8 }}); # Load the javascript when Internet Explorer lower than 8 or Opera 6 is used # <script type="text/javascript" src="http://domain/js_file.js"></script> <%= js_load('http://domain/js_file.js', {inplace => 1, browser => {"Internet Explorer" => 'lt 8', Opera => 6} }); # Load the javascript when Internet Explorer is not version 8 <%= js_load('http://domain/js_file.js', {inplace => 1, browser => {"Internet Explorer" => '!8' } } );
There's the "special" browser default. So you are able to load javascript for e.g. everything but IE6
# Load the javascript when Internet Explorer is not version 6 <%= js_load('http://domain/js_file.js', {inplace => 1, browser => {"Internet Explorer" => '!6', default => 1 } } );
When you use this module, a hook for after_render is installed. That hook inserts the <script> tag at the end of the document or right before the closing <body> tag.
<script>
<body>
To avoid that late loading, you can use inplace in the config:
<%= js_load( 'test.js', {inplace => 1} ) %>
Called when registering the plugin. On creation, the plugin accepts a hashref to configure the plugin.
# load plugin, alerts are dismissable by default $self->plugin( 'JSLoader' );
$self->plugin( 'JSLoader' => { base => 'http://domain/js', # base for all <script> tags });
This plugin uses the stash key __JSLOADERFILES__, so you should avoid using this stash key for your own purposes.
__JSLOADERFILES__
Renee Baecker <reneeb@cpan.org>
This software is Copyright (c) 2013 by Renee Baecker.
This is free software, licensed under:
The Artistic License 2.0 (GPL Compatible)
To install Mojolicious::Plugin::JSLoader, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Mojolicious::Plugin::JSLoader
CPAN shell
perl -MCPAN -e shell install Mojolicious::Plugin::JSLoader
For more information on module installation, please visit the detailed CPAN module installation guide.