HTML代码:
<form id="form">
<input value="./iconfont.woff" name="url" required is="ui-input">
<button is="ui-button" data-type="primary">检测</button>
</form>
<p id="result"></p>
JS代码:
form.addEventListener('submit', function (event) {
event.preventDefault();
// 地址
var url = this.querySelector('[name]').value;
// 兼容性判断,防止 IE 浏览器下报错
if (window.FontFace) {
var fontFile = new FontFace('Some Font Name', 'url(' + url + ')');
fontFile.load().then(function () {
result.innerHTML = '<span style="color:green">加载成功!</span>';
}, function (err) {
result.innerHTML = '<span style="color:red">加载失败:' + err + '</span>';
});
}
});