I need help regarding on how to determine a file type using JavaScript. For example in this html.
<div class="indi-download">
<div class="pull-left">
<h6 class="file-format">%file_display_name%</h6>
</div>
<div class="pull-right">
<a class="download-link" href="%file_url%">Download</a>
</div>
on this line of code,
<a class="download-link" href="%file_url%">Download</a>
the href="%file_url%" will return the file type with the following extensions:example: .docx or .pdf.
something like this.
<a class="download-link" href="http://mysite.com/download/myfiles/sample.pdf">Download</a>
my problem is, I need to detect what type of file is that using javascript. And if it is a .pdf, the class class="file-format" will change into class="pdf-file-format" and class="doc-file-format" for .doc. It depends on the file extension. Any idea on how to do this? what must be the best trick to do this?
This is my sample output, I just want to change the icon of a file that's why I need to change the class name.
EDIT I did something like this but it doesn't work if there are multiple items on the list. Its only working on the first item. please help me.
<body onload="myFunction()">
<div class="indi-download">
<div class="pull-left">
<h6 class="pdf-file" id="file-display-id">%file_display_name%</h6>
</div>
<div class="pull-right">
<a class="download-link" id="download_link" href="%file_url%">Download</a>
</div>
</div>
</body>
JavaScript
<head>
<script type="text/javascript">
function myFunction()
{
var url = document.getElementById("download_link").href;
var ext = url.split('.').pop();
if(ext=='pdf'){
var newfileclass = document.getElementById("file-display-id");
newfileclass.className="pdf-file";
}
else if((ext=='docx')){
var newfileclass = document.getElementById("file-display-id");
newfileclass.className="pdf-file doc-file";
}
else if(ext=='zip'){
var newfileclass = document.getElementById("file-display-id");
newfileclass.className="pdf-file zip-file";
}
else{
var newfileclass = document.getElementById("file-display-id");
newfileclass.className="other-file";
}
}
</script>
</head>