Javascript Browser Detect with IE 11

1.3k views Asked by At

I am trying to fix some javascript code that I am currently using on a website - which was working perfectly until IE 11 came along. The script is used to first detect the type of browser used then autofills the name of a file (like a JPEG or PDF) into a specified textfield, after the file has been uploaded to the site. However I am not the original author of this script and I am very unfamiliar with Javascript. I have searched everywhere for a solution and so far have found nothing to help me with this problem. Hoping someone can help. Here is the current code:

<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> 
1

There are 1 answers

0
user3067421 On

After thinking about your comments about browser detection, I decided to play with the code a little and discovered that it's not needed and works great now. Thanks Eric Barber for your help in pointing me in the right direction! Here is the amended code:

<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>