The below function works fine on Opera, Firefox and Chrome. However, in IE8 it fails on the if ( allowed.indexOf(ext[1]) == -1)
part.
Does anyone know why? Is there any obvious mistake?
function CheckMe() { var allowed = new Array('docx','xls','xlsx', 'mp3', 'mp4', '3gp', 'sis', 'sisx', 'mp3', 'wav', 'mid', 'amr', 'jpg', 'gif', 'png', 'jpeg', 'txt', 'pdf', 'doc', 'rtf', 'thm', 'rar', 'zip', 'htm', 'html', 'css', 'swf', 'jar', 'nth', 'aac', 'cab', 'wgz'); var fileinput=document.getElementById('f'); var ext = fileinput.value.toLowerCase().split('.'); if ( allowed.indexOf(ext[1]) == -1) { document.getElementById('uploadsec').innerHTML = document.getElementById('uploadsec').innerHTML; alert('This file type is not allowed!'); } }
IndexOf(Array, Object, Int32) Searches for the specified object in a range of elements of a one-dimensional array, and returns the index of its first occurrence. The range extends from a specified index to the end of the array.
The indexOf() method returns the first index at which a given element can be found in the array, or -1 if it is not present.
To find the position of an element in an array, you use the indexOf() method. This method returns the index of the first occurrence the element that you want to find, or -1 if the element is not found.
The indexOf() method returns the first index (position) of a specified value. The indexOf() method returns -1 if the value is not found. The indexOf() method starts at a specified index and searches from left to right. By default the search starts at the first element and ends at the last.
Versions of IE before IE9 don't have an .indexOf()
function for Array, to define the exact spec version, run this before trying to use it:
if (!Array.prototype.indexOf) { Array.prototype.indexOf = function(elt /*, from*/) { var len = this.length >>> 0; var from = Number(arguments[1]) || 0; from = (from < 0) ? Math.ceil(from) : Math.floor(from); if (from < 0) from += len; for (; from < len; from++) { if (from in this && this[from] === elt) return from; } return -1; }; }
This is the version from MDN, used in Firefox/SpiderMonkey. In other cases such as IE, it'll add .indexOf()
in the case it's missing... basically IE8 or below at this point.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With