我想知道如何检查具有特定文件的网址是否具有 pdf 扩展名,在我的代码中,如果该网址具有 pdf 文件,则会打开模式,如果是任何其他扩展名,则不会发生任何情况。
我尝试使用以下方法执行此操作:if (/pdf/.test(document.file))
作为 document.file 来 self 的文件的链接。
但是有些问题,也许代码的语法我应该与html部分中的+和“”混淆。
我的代码是:
for(var key in data.val()){
documento = data.val()[key]
linha = "<tr>"+
"<td>"+documento.titulo+"</td>"+
"<td>"+documento.data_inicio+"</td>"+
"<td>"+documento.categoria+"</td>"+
if (/pdf/.test(documento.arquivo)){+
"<td class='view'><a data-toggle='modal' data-target='#myModal' data-link='"+documento.arquivo+"'><i class='fa fa-eye'></i></a></td>"+
}+
"</tr>";
$('#tableCustom tbody').append(linha);
$(".view a").on( "click", function() {
var link = $(this).data('link');
PDFObject.embed(link, ".modal-content");
});
}
最佳答案
您可以使用 .indexOf() ,它会告诉您网址中是否有“.pdf”
if (documento.arquivo.indexOf(".pdf") != -1) {
// It is a pdf
}
关于javascript - 验证 url 是否有 pdf 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47964310/