October 12, 2007 2

#include for JavaScript Source Files Using Prototype

By Levi Senft in Web

I’ve seen many posts over the years asking if there is a javascript equivalent to the cc++ #include command, or Java’s import command for including source files. Today I decided to whip one up using Prototype. Check out the demo here.

Here is my HTML page. The code for the include statement is embedded at the top of the page. The code using the statement is in a block at the bottom of the page.

    1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    2     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    3 <html xmlns="http://www.w3.org/1999/xhtml">
    4     <head>
    5         <title>
    6             #include statement for prototype/scriptaculous
    7         </title>
    8         <script type="text/javascript" src="prototype.js"></script>
    9         <script type="text/javascript">
   10 //<![CDATA[
   11
   12         function include (jsFile) {
   13             new Ajax.Request(jsFile, {
   14                 method: 'get',
   15                 asynchronous  : false,
   16                 onSuccess: function(transport) {
   17                     // Evaluate the javascript
   18                     eval(transport.responseText);
   19                 },
   20
   21                 onFailure : function() {
   22                     alert("Failure including file: " + jsFile);
   23                 }
   24
   25             });
   26
   27         }
   28
   29         //]]>
   30         </script>
   31     </head>
   32     <body>
   33         <h1>
   34             #include statement for javascript
   35         </h1>
   36         <h2>
   37             The Prototype #include statement:
   38         </h2>
   39         <pre>
   40             function include (jsFile) {
   41                 new Ajax.Request(jsFile, {
   42                     method: 'get',
   43                     asynchronous  : false,
   44                     onSuccess: function(transport) {
   45                         // Evaluate the javascript
   46                         eval(transport.responseText);
   47                     },
   48
   49                     onFailure : function() {
   50                         alert("Failure including file: " + jsFile);
   51                     }
   52
   53                 });
   54
   55             }
   56 </pre>
   57         <h2>
   58             The javascript file I'm including:
   59         </h2>
   60         <p>
   61             hello.js
   62         </p>
   63         <pre>
   64
   65             sayhello = function () {
   66                 alert("I'm saying hello!");
   67             }
   68
   69 </pre>
   70         <h2>
   71             Using the include statement:
   72         </h2>
   73         <pre>
   74
   75 include('hello.js');
   76
   77 sayhello();
   78
   79 </pre>
   80 <script type="text/javascript">
   81 //<![CDATA[
   82
   83         include('hello.js');
   84
   85         sayhello();
   86
   87         //]]>
   88         </script>
   89     </body>
   90 </html>
   91 

The var command takes the function out of the global scope for some reason. Here is a demo with the non-working code.

October 6, 2007 1

Generating a Sine Table With JavaScript

By Levi Senft in Web

I was looking at a C tutorial on Drexel University’s site when I saw an example for generating a sine table. I decided to try adapting this little program to JavaScript. I ended up creating a rudimentary table object to render the data in a HTML table with a caption and header. The calculations use [...]

September 23, 2007 6

Shell Script to Delete .svn Folders

By Levi Senft in Mac

This script uses the rm and find commands to recursively delete .svn folders. rm and find can be found on all UNIX based operating systems including Linux and OS X. 1 #!/bin/sh 2 3 echo “recursively removing .svn folders from” 4 pwd 5 rm -rf `find . -type d -name .svn` 6

September 23, 2007 0

Shell Script to Search for Inline Styles

By Levi Senft in Mac

A few months back I was working on a user interface project that had to be cleaned up for production. One of my jobs was to consolidate all the styles into organized style sheets. The application had hundreds of pages several with inline styles. For a quick way to search the html files for inline [...]

September 22, 2007 2

OS X Codecs

By Levi Senft in Mac

I just got season one of Torchwood and wanted to convert the episodes to a format that my Video iPod can handle. The episodes where encoded as avi files, which quicktime can’t handle out of the box. So I went to find codecs when I stumbled on Shaun from Twenty08‘s blog post with a link [...]

September 22, 2007 3

JavaScript Include

By Levi Senft in Web

A few months ago at Humaniz we were working on a user interface project for an existing ASP.NET application. As a short cut, some of our developers use PHP includes when building templates to cut down down on the amount of duplicated code during the early stages of development. The client’s developers wanted to take [...]

July 13, 2007 0

Updated CGI Script Troubleshooting

By Levi Senft in Web

I wanted a larger view area for the CGI troubleshooting script. This throws the output into a text area that fills the entire browser window. #!/usr/bin/perl print “Content-type: text/htmlnn”; print “”; print “”; print “”; system (“python MyPythonScript.py 2>&1n”); print”"; print “”; exit();

Tags: , , ,

May 20, 2007 0

Start, Stop and Restart Apache on Mac OS X

By Levi Senft in Mac

The apachectl command can be found in /usr/sbin, so to start apache: sudo /usr/sbin/apachectl start   To Stop Apache:  sudo /usr/sbin/apachectl restart To Restart Apache: sudo /usr/sbin/apachectl restart

Tags: , , ,

May 20, 2007 0

Refreshing /etc/hosts on OS X

By Levi Senft in Web

On most UNIX/Linux systems I’ve used changes to /etc/hosts are real time. In OS X you most reload the hosts file. This can be done with a single command rather than rebooting your machine. sudo niload -v -m hosts . < /etc/hosts

Tags: , , , ,

May 16, 2007 0

A PERL Script for Troubleshooting CGI Scripts

By Levi Senft in Web

I started working on a Python program that is hosted on GoDaddy. Much to my dismay GoDaddy doesn’t provide shell logins. I was testing the scripts on my machine, but I don’t have the mysql library installed. When I started to write the database portion of the script I wrote this PERL CGI script to [...]

Tags: , , ,