Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dropzone.js is giving error "Invalid dropzone element" on page without a dropzone

I'm using dropzone js and it's working great on pages that I require a dropzone. On any other page though it's giving me a "Invalid dropzone element" error message and causing issues with my other javascript.

I have a custom JS file (which loads immediately after the dropzone.js file) and at the very top of the file I have the following line of code:

Dropzone.autoDiscover = false;

This should stop it from looking at any page where I'm not enabling it programatically. The error only goes away on pages where there is a valid dropzone.

I also set the following code on line 1470 on dropzone.js to try and enable it there too:

Dropzone.autoDiscover = false;

How can I get rid of this error?

like image 883
user1048676 Avatar asked Jan 09 '15 22:01

user1048676


People also ask

Why is there no URL provided for my dropzone?

Uncaught Error: No URL provided. Uncaught Error: No URL provided happens when a Dropzone gets attached to an object without an action attribute on a form or a JavaScript configuration for specific Dropzone. This problem is caused if a Dropzone is attached to an element before configuring it via JavaScript.

What is dropzonejs?

Use the tag dropzonejs and there'll be plenty of people helping you out. Dropzone is a robust library trusted by millions. It started in 2012 with compatibility in mind. Whether your users use an outdated browser, or have JavaScript enabled, Dropzone got you covered.

Where can I find dropzone documentation?

All the documentation about Dropzone, and the multiple ways to configure and customise it, can be found on GitBook. If you need help, there are GitHub Discussions and Stackoverflow. Use the tag dropzonejs and there'll be plenty of people helping you out. Dropzone is a robust library trusted by millions.

How to check if a Div exists before init dropzone?

You can check if the element is there, before you init dropzone. With jQuery you can try something like this: if ($ ('#dropzoneDiv').length) { initDropZoneHere } @Philip Where would I put this code? Do you use jQuery or pure JS? The idea is that you can check if the div exists before you init Dropzone.


Video Answer


2 Answers

With pure JS you can try this:

if (document.getElementById('DropzoneElementId')) {
  var myDropzone = new Dropzone("div#DropzoneElementId", { url: "/file/post"});
  // other code here
}

or if you use jQuery:

if ($('#DropzoneElementId').length) {
  $("div#DropzoneElementId").dropzone({ url: "/file/post" });
  // other code here
}
like image 121
Philip Avatar answered Oct 23 '22 01:10

Philip


Long time ago but: Put the script after your Form Element and the error is gone.

like image 3
VZimmerl Avatar answered Oct 23 '22 02:10

VZimmerl