Firefox won’t submit form not in DOM

December 8, 2015

Ah, differences between browsers. Am I right?

Today I learned that Firefox will not allow you to submit a form that hasn’t been attached to the DOM. Let’s take a look at a quick example.

(function() {
  var form = document.createElement('form');
  form.method = 'GET';
  form.action = 'https://path/to/download/file.zip';
  form.submit();
})();

This code will run as expected in Chrome, downloading file.zip as soon as you hit the page but it will not run in Firefox.

This is because Firefox won’t submit forms that aren’t in the DOM.

An easy fix here would be to just attach it to the DOM like so:

(function() {
  var form = document.createElement('form');
  form.method = 'GET';
  form.action = 'https://path/to/download/file.zip';
  document.body.appendChild(form);
  form.submit();
})();

And voilà!

( Also you could just use an anchor tag and force a click! )