0

I am having an odd problem in my onclick handler.

I am calling a javascript function "download" in the onclick of an . This has been in use on production websites for years. Recently, I get a strange javascript error when clicking the button in Firefox or Chrome (not a problem in IE8). Firefox says "TypeError: download is not a function" and Chrome says "TypeError: string is not a function".

HTML:

<a onclick="download('position','container','ids');return false;" href="#">Run download</a>

JS:

function download(position, container, ids) {
  alert('in download');
}

You can see this demonstrated in this Fiddle.

Primarily, I would like to know WHY this doesn't work (other functions work fine). It looks like if I rename the function or use a button instead of a link, the problem will be solved as well, but deploying such a change will be a nightmare. If it is necessary, that's fine, but I want to know why the download function no longer works.

thelr
  • 1,134
  • 11
  • 30
  • 1
    possible duplicate of [Can't use "download" as a function name in javascript](http://stackoverflow.com/questions/7852237/cant-use-download-as-a-function-name-in-javascript) – Quentin Apr 17 '13 at 13:25
  • Yes, yes it is. Thanks for identifying that. – thelr Apr 17 '13 at 13:38

1 Answers1

4

It seems that, with HTML5, <a> tags support a new attribute named "download" (see this link for example), that has an empty value by default.

Your code will work if you change it to:

<a onclick="self.download('position','container','ids');return false;" href="#">Run download</a>

Indeed, events (onclick in this case) run in the scope of the element they are bound to (<a> here), so "download" means this.download if it exists.

Walid
  • 1,262
  • 11
  • 10