Javascript浏览器检测与IE 11

Javascript Browser Detect with IE 11

本文关键字:IE 检测 浏览器 Javascript      更新时间:2024-03-05

我正在尝试修复我目前在网站上使用的一些javascript代码,在IE 11出现之前,这些代码一直运行良好。该脚本用于首先检测所使用的浏览器类型,然后在文件上传到网站后,将文件名(如JPEG或PDF)自动填充到指定的文本字段中。然而,我不是这个脚本的原始作者,我对Javascript非常陌生。我到处寻找解决方案,但到目前为止还没有找到任何帮助我解决这个问题的方法。希望有人能帮忙。这是当前代码:

<script langauge="javascript">
function post_value(){
    BrowserDetect.init();
 if(BrowserDetect.browser=='Explorer'){
    var str=document.formSmallPhoto.file1.value;
    var index=str.lastIndexOf("''");    
    opener.document.form1.small_photo.value = str.substring(index+1);   
 }
BrowserDetect.init();
 if(BrowserDetect.browser=='Explorer'){
    var str=document.formSmallPhoto.file1.value;
    var index=str.lastIndexOf("''");    
    opener.document.form1.small_photo.value = str.substring(index+1);   
 }
 if(BrowserDetect.browser=='Safari'){
    var str=document.formSmallPhoto.file1.value;
    var index=str.lastIndexOf("''");    
    opener.document.form1.small_photo.value = str.substring(index+1);   
 }
 if(BrowserDetect.browser=='Chrome'){
    var str=document.formSmallPhoto.file1.value;
    var index=str.lastIndexOf("''");    
    opener.document.form1.small_photo.value = str.substring(index+1);   
 }
 if(BrowserDetect.browser=='Opera'){
    var str=document.formSmallPhoto.file1.value;
    var index=str.lastIndexOf("''");    
    opener.document.form1.small_photo.value = str.substring(index+1);   
 }
 if(BrowserDetect.browser=='Firefox'){
    var str=document.formSmallPhoto.file1.value;
    var index=str.lastIndexOf("''");    
    opener.document.form1.small_photo.value = str.substring(index+1);   
 }
}
function closeChildWindow(){
    self.close();   
}   
</script> 

在考虑了您对浏览器检测的评论后,我决定稍微处理一下代码,发现它不需要,而且现在效果很好。感谢Eric Barber为我指明了正确的方向!这是修改后的代码:

<script langauge="javascript">
function post_value(){
    {
    var str=document.formSmallPhoto.file1.value;
    var index=str.lastIndexOf("''");    
    opener.document.form1.small_photo.value = str.substring(index+1);   
 }

}
function closeChildWindow(){
    self.close();   
}   
</script>