Internet Explorer Javascript Indexof Error
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Why doesn't indexOf work on an array IE8? up vote 266 down vote favorite 49 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!'); } } javascript internet-explorer internet-explorer-8 indexof share|improve this question edited Jul 28 at 16:09 reformed 1,44562347 asked Sep 2 '10 at 16:33 nLL 1,65163566 5 Great question, great answer. Thanks for giving me exactly what I needed. –Hardwareguy Sep 13 '11 at 19:36 add a comment| 7 Answers 7 active oldest votes up vote 460 down vote accepted 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. share|improve this answer edited Sep 10 at 3:40 Gras Double 5,98932230 answered Sep 2 '10 at 16:38 Nick Craver♦ 432k8410601011 2 Note the caveat that if you (or libraries you use) use
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like http://stackoverflow.com/questions/3629183/why-doesnt-indexof-work-on-an-array-ie8 you, helping each other. Join them; it only takes a minute: Sign up IndexOf not Supported in IE8 Browser up vote 7 down vote favorite 1 I have cascaded dropdown in my application, cascaded using jquery, now my problem is it is working smoothly with IE9, Firefox, Opera and Safari but does not work with any of http://stackoverflow.com/questions/8848097/indexof-not-supported-in-ie8-browser browsers like IE7,IE8 etc. I surfed for the problem and found that error is due to indexOf in my jquery code, i tried it by removing indexOf but still it is giving the same error.. Note: Is there any work around in telerik script to remove indexOf, coz new only i can find indexOf in their script. Below is the Code: function OnClientSelectedIndexChanged(sender, eventArgs) { var senderId = sender.get_id().toString(); var uniqueName = senderId.substring(senderId.lastIndexOf('_'), senderId.length); if(senderId.indexOf("drpdwnCondition") > 0) { return false; } var selectedItem = eventArgs.get_item(); var selectedValue = selectedItem.get_value().split('_'); $.ajax({ type: "POST", async: true, url: "/SalesRepresentativeMonitoring.aspx/GetData", contentType: "application/json; charset=utf-8", data: "{value:" + JSON.stringify(selectedValue[1]) + "}", dataType: "json", success: function (msg) { var resultAsJson = msg.d // your return result is JS array // Now you can loop over the array to get each object var cnditionCombo = $find("ctl00_ContentPlaceHolder1_radDock_C_Filter_drpdwnCondition" + uniqueName.toString()); cnditionCombo.clearSelection(); cnditionCombo.trackChanges(); cnditionCombo.clearItems(); for (var i in resultAsJson) { //alert(resultAsJson[i]); var item = new Telerik.Web.UI.RadComboBoxItem(); item.set_text(resultAsJson[i]); item.set_value(resultAsJson[i]); cnditionCombo.get_items().add(item); } var itemAtIndex = cnditionCombo.get_items().getItem(0); //get item in detailCB itemAtIndex.select(); cnditio
existence of an Array member in
Array.prototype.indexOf = function (obj, start) {
for (var i = (start || internet explorer 0); i < this.length; i++) {
if (this[i] == obj) {
return i;
}
}
return -1;
}
} Like this:Like Loading... Related « Bristol Bridal Boutique IE8 Expected IdentifierError » No comments yet Posting your comment. Leave a Reply Cancel reply Enter your comment here... Fill in internet explorer javascript your details below or click an icon to log in: Email (required) (Address never made public) Name (required) Website You are commenting using your WordPress.com account. (LogOut/Change) You are commenting using your Twitter account. (LogOut/Change) You are commenting using your Facebook account. (LogOut/Change) You are commenting using your Google+ account. (LogOut/Change) Cancel Connecting to %s Notify me of new comments via email. Recent Posts Bristol JS talk - ReactJS & ReactNative Javascript Frameworks' LongevityArticle Developing E-Learning Web Applications in the Multi-Platform DigitalLandscape IE8 Expected IdentifierError IE7 and IE8's indexOf() ArrayBug Tag Cloud Adobe Amsterdam AS3 award Backbone.js Box2D Bristol bug bullets CDATA co-working code CreateJS creative CS4 development digital e-learning Eclipse Enterprise Zone error FD FDT FITC Flash font FOTB frameworks freeelance freelance freelancing Games Glastonbury graphics GSAP haXe html HTML5 htmlText IDE IE illustration indexOf interactive Internet iPad iPhone Javascript Jeash Jelly jQuery mac marionette.js MovieClip NME performance Photography PureMVC sass Snow Leopard Spike Design Spike Island svn Temple Quarter Temple Studios trace Tween UML UX VMWare Fusion Web Wedding dresses windows Word-wrap XML Archives April 2016 August 2015 July 2015 June 2014 May 2014 October 2013 July 2013 March 2013 October 2012 June 2012 April 2012 March 2012 November 2011 October
(1)Doodles (2)General (30)GitHub (1)Graphics (15)HTML5 (5)Javascript (34)jQuery (13)Lion (2)Mobile Tricks (1)MS-SQL (1)MySQL (2)Photography (1)PHP (2)Railo (8)Snow Leopard (2)Spotlight (18)Web Development (47)Blog ArchivesDecember 2012July 2012December 2011November 2011July 2011June 2011May 2011April 2011February 2011January 2011December 2010November 2010October 2010September 2010July 2010May 2010April 2010March 2010February 2010January 2010December 2009September 2009August 2009July 2009June 2009April 2009March 2009February 2009January 2009November 2008April 2008February 2008July 2006April 2006September 2005January 2005Jun 094indexOf problems in Internet Explorerjavascript0 commentsReason #592 why I hate Internet ExplorerMy last step on any website design is to go through it and make sure that it works and look correct in the various flavors of Internet Explorer. I can't really say that the time spent doing this is blissful. I compare it to going to the dentist, because I never know what I'm going to find and when I do it's rarely good.So today as I was debugging, what I thought was a very simple, JavaScript page. I came across one of those nifty "This method or function is undefined" errors from the Internet Explorer debugger. And as we all know, the IE debugger is as worthless as playing "I Spy" with a blind person.So what was the problem? Well it turns out that IE doesn't like indexOf for arrays. I did a little research and I came across a fix online from Soledad Penades Blog. Basically, we need to test if the indexOf method is defined for Arrays. If not, then use the much loved JavaScript prototype function to create it, like so:if(!Array.indexOf){ Array.prototype.indexOf = function(obj){ for(var i=0; i