Logic not working as expected.

Logic not working as expected.

classic12classic12 Posts: 228Questions: 60Answers: 4

Hi guys I have the following code which checks for the extension type and display accordingly.

The first section checks to see if it is an image.
The second if it is a video.
The third show id none of the above.

It is failing on ext = JPG ( ie it shows as a link ).

What am I doing wrong here please?

            render: function(data, type, row)
              {
                   
           
            if (data !== undefined ){ 
            
                 var ext = data.substr(data.lastIndexOf('.') + 1);
                    if ( (ext === 'jpeg' ) || ( ext === 'jpg' ) || ( ext === 'JPG' ) || ( ext === 'jp2' ) || ( ext === 'png' )|| ( ext === 'gif' ) || ( ext === 'pdf' ))
                        {
                           
                            return '<img src=" ' + data + ' "' + imageSize + ' class="info1">';
                           
                        }
 // now check if it is a video                     
                 if ( (ext === 'mp4')|| ( ext === 'MOV' ))
                    {
                          return ' <video width="320" height="240" controls>  <source src=" ' +data + ' " type="video/mp4">  <source src="movie.ogg" type="video/ogg">  Your browser does not support the video tag.   </video>'
                     }
 // last here so must be download                    
                    {
                          var fullPath = data.split('.')[0];
                          var filename = fullPath.replace(/^.*[\\\/]/, '');  
                          return ' Click to Download <br/> <a href=" '+ data + ' " target="_blank"  > '+ data +  " </a> ";
                }
             }   else { return '' };
                            }
                           
          } 

This question has an accepted answers - jump to answer

Answers

  • rf1234rf1234 Posts: 2,991Questions: 87Answers: 421
    edited December 2017 Answer ✓

    Not sure whether I really understood your problem but maybe you want to try this:

    var ext = data.toLowerCase().trim().substr(data.lastIndexOf('.') + 1);
    

    This removes whitespace from your substring and converts it to lower case.
    Consequently you need to make these changes as well:

    if ( (ext === 'jpeg' ) || ( ext === 'jpg' ) || ( ext === 'jp2' ) || ( ext === 'png' )|| ( ext === 'gif' ) || ( ext === 'pdf' ))
    
    if ( (ext === 'mp4')|| ( ext === 'mov' ))
    

    https://www.w3schools.com/jsref/jsref_trim_string.asp
    https://www.w3schools.com/jsref/jsref_tolowercase.asp

  • classic12classic12 Posts: 228Questions: 60Answers: 4

    Thanks,

    that did the job but still not sure why....

    Can't see any white space and I had JPG.

  • rf1234rf1234 Posts: 2,991Questions: 87Answers: 421

    Glad that it helped! The white space can have been something invisible after the JPG. trim() removes whitespace before and after.

This discussion has been closed.