jeudi 9 avril 2015

How to stop barcode scanner focusing address bar

I'm using some jQuery code on our site that should bring up an alert when the tab / enter button is selected. Using a barcode scanner I am emulating an enter click after the data has been grabbed by the scanner.


(the overall goal IS to redirect to a users data page when they have an emailed barcode scanned but that's not important to this test)


The issue I'm having is that the data is either not bringing up the alert or it is writing straight to the address bar / going back to a previous page / home page. (it seems to depend on what barcode type is being scanned.)


How do I stop the focus area being the address bar on the scanner emulating an enter click?


This is my jQuery



$(document).ready(function() {
var barcode = "";
$(document).keydown(function(e) {
var code = (e.keyCode ? e.keyCode : e.which);
if (code == 13) { // Enter key hit
alert(barcode);
}
else if (code == 9) { // Tab key hit
alert(barcode);
}
else {
barcode = barcode + String.fromCharCode(code);
}
});
});


Taken with credit from : Barcode scanner for html5 and jquery application


Aucun commentaire:

Enregistrer un commentaire