Tuesday, January 23, 2007

Perl Mason handles Javascript incorrectly?

I installed Apache2 with Perl/Mason on a machine, and right off the bat it seemed to handle some file types, .java and .js, incorrectly. I didn't realize what was happening at first, so to test the setup, I added a small Mason file, test.html, to the /mason_example directory. The contents of that file were as follows:
<%args>
$hello => ''
</%args>

<script src="test.js" type="text/javascript"></script>
<script type="text/javascript" charset="utf-8">
<%perl>
if (!$hello) {
$hello = "hello";
}
</%perl>
alert("from script: <% $hello %>");
hello("<% $hello %>");
</script>

Here is the content of test.js:
var hello  = function(str) {
alert(str);
}
When I hit test.html in a browser (e.g. http://www.example.com/mason_example/test.html), I would see the from script:.. alert, but the Javascript console would display an error indicating that test.js was not being interpreted as Javascript:
Error: syntax error
Source File: http://www.example.com/mason_example/test.js
Line: 1
Source Code:

Error: hello is not defined
Source File: http://www.example.com/mason_example/test.html?hello=hello
Line: 19
After doing a bit of research, I found a solution at MasonHQ:
What's needed is a simple way for mason to be told to try and handle everything that is either html or txt or cgi or in fact anything but some kind of media file or css or js.
I used the workaround suggested there. I changed the FilesMatch stanza in libhtml-mason-perl-examples.conf as follows:
<Directory /var/www/mason_example>
<FilesMatch "(\.html|\.txt|^[^\.]+)$>
# <FilesMatch [^/]>
SetHandler perl-script
PerlResponsehandler HTML::Mason::ApacheHandler
# Only CGI is supported with mod_perl2 currently
PerlSetVar MasonArgsMethod CGI
</FilesMatch>
<FilesMatch "\.(js|css|java)$">
SetHandler default-handler
</FilesMatch>
<Directory>
Now my Javascript files are working, hooray!

0 Comments:

Post a Comment

<< Home