It wasn't as hard as you might think it is... it truly isn't... to "program" the scanners with a preamble, it's just a matter of scanning a series of configuration barcodes from the manufacturer. Takes me about 2 minutes to get a new scanner setup for our system.
Here is the JavaScript I use to handle the scanner... it may seem daunting, but if you look at it, you'll see that there is nothing really special about my code. You could pretty much copy what I have and change the object paths to your own. If you have an image for an hourglass, the code below includes a popup div to alert the individual scanning that the barcode was scanned properly (and since the postamble character is a carriage return, the hourglass alerts the individual to wait while the form is submit). This code also prevents the individual from scanning the same barcode twice. You may or may not want this (it can be changed in the bcCheck function). For my purposes it would have been bad.
I hope this helps..
Code:
<script>
<!--
var sLastBC
document.onkeypress = bcScan;
document.onkeyup = bcClean;
//When a key is pressed, we'll check to see if it's the preamble from the scanner
function bcScan(e){
switch (event.keyCode){
//Compare the Ascii value of the global variable we set in the system for the preamble with that of the current character.
case <%=Asc(gbl_BCPreAmble)%>:
//This character is the preamble, set the focus prepare the field for data...
document.frmOrderViewerSearch.query.focus();
document.frmOrderViewerSearch.query.value = "";
}
}
function bcClean(e){
//Clean out any character from value that was entered from the scanner that matches the preamble character.
var RegEx = /<%=gbl_BCPreAmble%>/g;
document.frmOrderViewerSearch.query.value = document.frmOrderViewerSearch.query.value.toString().replace(RegEx, "");
}
//This function is called on the onSubmit of the form. It validates the form fields, and displays an hourglass box to alert the individual that the scan was successful. This entire section from here down is not necessary for the barcode to be successfully scanned.
function bcCheck(){
if(document.frmOrderViewerSearch.query.value != ""){
if(sLastBC != document.frmOrderViewerSearch.query.value){
sLastBC = document.frmOrderViewerSearch.query.value;
if ( bHourglassAvail ) {
showHourglass();
return true;
}
else{
return false;
}
}
else{
alert("Please enter a transaction ID.");
return false;
}
}
//The following code handles the hourglass. It ensures that the image of the hour glass stays in the middle of the window, even through resizing.
var bHourglassAvail = false;
window.onresize = updateHourglassLoc;
function updateHourglassLoc() {
if(bHourglassAvail == true){
document.getElementById("hourglass").style.pixelTop = (document.body.clientHeight / 2) - (document.getElementById("hourglass").height / 2);
document.getElementById("hourglass").style.pixelLeft = (document.body.clientWidth / 2) - (document.getElementById("hourglass").width / 2);
}
}
//Shows the div containing the hourglass
function showHourglass(){
if(bHourglassAvail == true){
updateHourglassLoc();
document.getElementById("hourglass").style.display = "";
}
}
//Hides the div containing the hourglass
function hideHourglass(){
if(bHourglassAvail == true){
document.getElementById("hourglass").style.display = "none";
}
}
//-->
</script>
<!-- div for the hourglass image itself. not necessary for barcode scanning to function properly -->
<div id="hourglass" style=" position:absolute; border:1px solid red; background-color:#EFEFEF; text-align:center; vertical-align:middle; display:none;" height="70" width="50">
<img id="hourglass-IMG" src="<%=sAppPath%>_images/hourglass.gif" height="50" width="50"><BR>
<span style="color:red; font-family:Arial, Helvetica, sans-serif; font-size:8px; width:50px; text-align:center;">Please<BR>Wait</span>
</div>
Note:
document.frmOrderViewerSearch.query.value = document.frmOrderViewerSearch.query.value.toString ().replace(RegEx, "");
This line, for some reason or another, is putting a space after toString and (). It's not here when I edit this message, so it's being added afterwards. Make sure to clean that up if you copy and paste the code. It shouldn't affect the code, but it looks sloppy...