我正在编写一个基于 [ libphonenumber ] 的简单电话号码解析器。不幸的是,“http://closure-library.googlecode.com/svn/trunk/closure/goog/base.js”需要永远加载,当我获取文件并将其包含为 src="base.js" ,弹出一堆错误。
我的猜测是,这是因为该库尚未加载,因此 goog.require() 语句失败。
我能做些什么?
<!DOCTYPE html>
<html>
<head>
<title>Phone Number Parser</title>
<script src="http://closure-library.googlecode.com/svn/trunk/closure/goog/base.js"></script>
<script>
goog.require('goog.dom');
goog.require('goog.json');
goog.require('goog.proto2.ObjectSerializer');
goog.require('goog.string.StringBuffer');
</script>
<script src="phonemetadata.pb.js"></script>
<script src="phonenumber.pb.js"></script>
<script src="metadata.js"></script>
<script src="phonenumberutil.js"></script>
</head>
<body>
<script>
numbers = ['6509066389', '+40723875777', '720-935-6433', '914-262-7178', '7123040634'];
for (i in numbers) {
console.log(format_for_five9(numbers[i]));
}
function format_for_five9(phoneNumber) {
var $ = goog.dom.getElement;
var regionCode = 'US';
var output = new goog.string.StringBuffer();
try {
var phoneUtil = i18n.phonenumbers.PhoneNumberUtil.getInstance();
var number = phoneUtil.parseAndKeepRawInput(phoneNumber, regionCode);
number_json_serial = goog.json.serialize(new goog.proto2.ObjectSerializer(goog.proto2.ObjectSerializer.KeyOption.NAME).serialize(number));
number_json = goog.json.parse(number_json_serial);
if(phoneUtil.isValidNumberForRegion(number, regionCode)) {
five9_format = number_json.national_number.toString();
}
else {
five9_format = number_json.country_code.toString() + number_json.national_number.toString();
}
} catch (e) {
output.append('\n' + e);
console.log(e);
}
return five9_format;
}
</script>
</body>
</html>