A partial archive of discourse.wicg.io as of Saturday February 24, 2024.

Invoke download without dispatching event

AshleyScirra
2016-05-11

It looks like browsers are moving away from allowing JS-dispatched events from performing their default actions. (e.g.: crbug.com/520519)

AFAIK the only way to invoke a download from JS is the following steps:

  1. Create a temporary <a> and attach it to the document
  2. Set the “href” and “download” attributes
  3. Dispatch a synthetic “click” event at it
  4. Remove the temporary <a>

Perhaps there should be a navigator.download(url, filename) method or similar to do the same thing but without having to rely on synthetic events? It would also be a much nicer API than muddling with temporary elements!

AshleyScirra
2016-05-11

Actually I was just informed that element.click() can work instead of dispatching an event. But maybe there should be a nicer API for this anyway?