为什么chrome扩展无法提供正确的输出?

时间:2018-04-01 14:57:09

标签: javascript google-chrome if-statement

使用以下javascript运行我的Chrome扩展程序。当电话号码格式不正确时,Chrome扩展程序会抛出正确的输出。但是当电话号码格式正确时,Chrome扩展名不会输出任何输出。试图将电话号码设为555-858-8585

var pbValues = {
projectName : 'kittenbook',
versionNumber : '0.0.1',
areaCodes: {
'408': 'Silicon Valley',
'702': 'Las Vegas',
'801': 'Northern Utah',
'765': 'West Lafayette',
'901': 'Memphis',
'507': 'Rochester, MN'
 },

currentDate : new Date(),   

// currentTime will look like '2014-01-25 at 14:45:12'
currentTime : currentDate.getFullYear() + '-' + // Set year
              (currentDate.getMonth() + 1) + '-' + // Set month
              currentDate.getDate() + ' at ' +    // Set day of the month
              currentDate.getHours() + ' : ' +    // Set hours (military time)
              currentDate.getMinutes() + ' : ' +  // Set minutes
              currentDate.getSeconds()           // Set seconds

};


var userName = prompt ('Hello, what\'s your name?');
var phoneNumber = prompt ('Hello' + userName + ',what\'s your phone number?');
var phoneNumberPattern = /1?-?\(?\d{3}[\-\)]\d{3}-\d{4}/;

var output = '<h1>Hello, ' + userName + '!</h1>';

if (phoneNumberPattern.test(phoneNumber)) {
output = output + '<p>' + pbValues.projectName + ' ' + pbValues.versionNumber 
+
                    ' viewed on: ' + pbValues.currentTime + '</p>'; 
} else {
output = output + '<h2> That phone number is invalid: ' + phoneNumber;
}
document.body.innerHTML = output;

0 个答案:

没有答案