advertisement
Premier Club Log In/Registration
  Include Code  Search Tips
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   SKILLBUILDING  |   TIP BANK  |   SOURCEBANK  |   FORUMS  |   NEWSLETTERS
Browse DevX
Download the code for this article
Partners & Affiliates
advertisement
advertisement
advertisement
Average Rating: 3/5 | Rate this item | 2 users have rated this item.
Rediscover (and Structure) CGI with a Simple Link Technique (cont'd)
Mult in Javascript
Syntactically, JavaScript is fairly similar to the PHP and Perl implementations. However, this implementation of Mult runs entirely on the client side.

Again, the multiplication routine is as simple as can be:


function mult( a, b )
{
  var prod = a * b;
  document.write( "<center>" );
  document.write( "<h1><b>" );
  document.write( a+" x "+b+" = "+prod );
  document.write( "</b></h1>" );
  document.write( "</center>" );
}
advertisement
Likewise, the generation of the multiplication table is straightforward:

document.write( "<center>" );
document.write( "<table border=\"1\">" );
document.write( "<tr>" );
document.write( "<td>X</td>" );
for (var c=0; c<10; ++c) {
  document.write( "<td>"+c+"</td>" );
}
document.write( "</tr>" );
for (var r=0; r<10; ++r) {
  document.write( "<tr>" );
  document.write( "<td>"+r+"</td>" );
  for (var c=0; c<10; ++c) {
    var link = alLink( "mult", r, c );
    document.write( "<td> <a href=\""+link+"\">"+r+" x "+c+"</a></td>" );
  }
  document.write( "</tr>" );
}
document.write( "</table>" );
document.write( "</center>" );
The link is defined in more or less the same way as in PHP and Perl:

var link = alLink( "mult", r, c );
One difference with the JavaScript implementation is that, in alinvoke.html, you must include the active link library after the definition of the mult() routine:

<script language="javascript" src="activeLink.js"></script>
Otherwise, the mult() routine will not be defined when alExtractAndApply() is called.

Freezing and Thawing in JavaScript
To encode the function call in JavaScript, take a list containing the function named and arguments and call toSource() on it, which returns a piece of Javascript source code. Escape this code with the escape() function:


var enc = escape( info.toSource() );
This code, when evaluated, will return the original list:

var info = eval( unescape( results[1] ) );
alApply( info );
Next, apply the first element of the list to the rest of the elements of the list:

function alApply( info )
{
  var fun = this[info[0]];
  var funargs = new Array();
  for (var i=1; i<info.length; ++i) {
    funargs[i-1] = info[i];
  }
  fun.apply( null, funargs );
Previous Page: Freezing and Thawing in PHP Next Page: Mult in Java


Page 1: IntroductionPage 4: Freezing and Thawing in PHP
Page 2: Something BetterPage 5: Mult in JavaScript
Page 3: Mult in PHPPage 6: Mult in Java
Please rate this item (5=best)
 1  2  3  4  5
advertisement
Advertising Info  |   Member Services  |   Permissions  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs