Javascript Undefined function
I am using electroni开发者_StackOverflow中文版c Signature Pad in my ASP.net application. I want to popup a alert message if the epad is not connected.
function isePad() {
var epad;
epad = window.document.esCapture1.ConnectedDevice;
alert(epad);
if (epad == '' ) {
alert('Sorry epad is not Connected or drivers not installed');
}
}
In most of the machines it is working fine. In some machine the first alert is showing undefined value. I want to display the alert('Sorry epad is not Connected or drivers not installed'); if epad value is '' or undefined
With this method epad
will evaluate to false
if it has a value of undefined
or ''
.
if (!epad) {
alert('Sorry epad is not Connected or drivers not installed');
}
function isePad() {
var epad;
epad = window.document.esCapture1.ConnectedDevice;
alert(epad);
if (epad == '' or epad == undefined) {
alert('Sorry epad is not Connected or drivers not installed');
}
}
Try
if (epad == '' || typeof epad == 'undefined') {
instead of
if (epad == '' ) {
So -- some of this is new to me and perhaps others will have valuable things to add to what I'm about to say...
Maybe try something like this...
(function() {
var epad = window.document.esCapture1 && window.document.esCapture1.ConnectedDevice;
alert(epad);
!epad && alert('Sorry epad is not Connected or drivers not installed');
})();
In your example, I think some or all browsers may choke on the 1st line within the function if 'window.document.esCapture1' is itself undefined, so I've changed it to guard against this possibility.
Also, putting this test in an anonymous function keeps the 'epad' variable out of the global namespace, which s a good idea (unless you need it elsewhere).
The last line simply tests to see if the 'epad' variable is falsy -- meaning the 2nd alert will appear if 'epad' is null, undefined, or false.
精彩评论