Friday, April 25, 2008 12:01 PM
by
loufranco
Writing Code 39 Barcodes with Javascript
I previously showed how easy it is to generate Code 39 Barcodes using DotImage, now I want to show you how to do it in pure JavaScript.
I wanted to make it easy to deploy so I don't use images. Luckily, 1D barcodes are easy to make with just colored table cells, so I just generate a one row table with a column for each bar. I set the width of the column to be the width of the bar and then color it either black or white.
Here's a page where you generate barcodes with just JavaScript. The code is attached to this entry.
To use it, just add Barcode.js to your site, include it in a script tag, then use the AtalasoftBarcode39 object to draw the barcode (there is a file called BarcodeTest.html in the zip file to show an example). Here's some sample code
<script src="Barcode.js" type="text/javascript"></script>
<script type="text/javascript">
function writeBC(s)
{
var bc = new AtalasoftBarcode39(s);
document.getElementById('bcArea').innerHTML =
bc.getBarcode(50, 2, 8);
}
writeBC("ABC123")
</script>
<div id="bcArea"></div>
The three arguments to getBarcode are the height and the widths of the narrow bars, and the width of the wide bars.