11540

Javascript function works in FF, Opera etc, fails in IE8 - how to fix

I have the following function (worked in IE6 but is broken in IE8)

function implode() { var str = ''; for(item in globvars) //<- IE8 wets itself here ... str+= '\n' + globvars[item]+';'; return str+'\n'; }

It seems an innocuous little function, but IE8 dosent grok it. Can anyone show me how to rewrite this so it work in IE8 (as well as the other browsers)?

<strong>[Edit]</strong>

At the begining of the script (i.e. first line after the tag, I have defined the globvars like this:

var globvars = new Array(); // This should give globvars global scope

The error from IE8 is:

Object does not support this action

Answer1:

I DO NOT have a variable named item anywhere else in my script

You will, however, have an element with id="item" or name="item". IE mirrors elements with an id/name not just as document.item but also window.item. (Obviously it's bad practice to rely on either.)

So when you say item= without telling it you want a var, silly IE thinks you're trying to assign to the existing HTMLElement with that id/name sitting in the window, and throws a fit because elements aren't writable. This is one reason to always use var even for global variables.

You shouldn't use for...in to iterate an Array. It doesn't do what you think. It may return the array items in the wrong order, and potentially unexpected non-numeric properties. Always use the plain old for (var i= 0; i<array.length; i++) loop for getting the items in an Array; for...in is only for Object used as a mapping.

Anyhow, JavaScript's built-in join() almost does the work for you:

function implode() { return '\n'+globvars.join(';\n')+';\n'; }

Answer2:

I use this syntax in IE all the time with no problem.

Have you tried:

function implode() { var str = ''; for(var item in globvars) //<- IE8 wets itself here ... str+= '\n' + globvars[item]+';'; return str+'\n'; }

If item is used globally elsewhere, it may be creeping in to your function's scope. Throwing in var scopes item to the current function.

Recommend

  • What would this code do? (memory management)
  • iterator_range in header file
  • In regards to the difference between Java “Properties” and C#' Properties
  • How to know which Linq statement produced the SQL on hand during runtime?
  • Vuejs: Lifecycle hooks of child routerview components using keep alive
  • IE6 changes DOCTYPE to a bad one
  • What command do i need to pass in SabreCommandLLSRQ to get current price of PNR?
  • JSON encode and decode on PHP
  • NHibernate manually control fetching
  • Updating Dojo provide
  • Defined variables not working in javascript files when I use getScript
  • Git describe fails to return most recent annotated tag
  • How can I display the parent menu item's description using Wordpress walkers?
  • NSScanner Loop Question
  • one Local Olampyad Questions on Informatic in 2011
  • Ajax Loaded meta Tags
  • Get one-time binding to work for ng-if
  • Linq Objects Group By & Sum
  • Javascript simulate pressing enter in input box
  • Azure Cloud Service Web Role web pages do not load
  • Optimizing database types to compact database (SQLite)
  • How do I fake an specific browser client when using Java's Net library?
  • How reduce the height of an mschart by breaking up the y-axis
  • Volley JsonObjectRequest send headers in GET Request
  • Perl system calls when running as another user using sudo
  • Importing jscolor library in angular 2
  • Rearranging Cells in UITableView Bug & Saving Changes
  • A cron job substitute?
  • Buffer size for converting unsigned long to string
  • KeystoneJS: Relationships in Admin UI not updating
  • Benchmarking RAM performance - UWP and C#
  • Hits per day in Google Big Query
  • Angular 2 constructor injection vs direct access
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Linking SubReports Without LinkChild/LinkMaster
  • XCode 8, some methods disappeared ? ex: layoutAttributesClass() -> AnyClass
  • Easiest way to encapsulate a HTML5 webpage into an android app?
  • reshape alternating columns in less time and using less memory
  • Net Present Value in Excel for Grouped Recurring CF