Angular directive preventing default action but Chrome still loading image on drag

Has anyone encountered an issue with an angular directive that is not successfully preventing Chrome's default action?

Below is the code for the directive in question:

app.directive('fileDrag', function () {
  return {
    restrict: 'A',
    link: function (scope, elem, attrs) {
      elem.bind('drop', function(e){
        e.preventDefault();
        console.log(e);
        return false;
      });
    }
  };
});

Here is the corresponding html snippet:

<div style="width: 500px; height:500px; background-color: red;" file-drag></div>

I am facing difficulties getting Chrome to refrain from opening the image in a new tab when dragging it onto the specified element. Any suggestions on how to resolve this issue?

Answer №1

It is important to always remember to preventDefault when handling the dragover event. To ensure proper functionality, update your code as shown below:

app.directive('fileDrag', function () {
  return {
    restrict: 'A',
    link: function (scope, elem, attrs) {
      elem.bind('drop', function(e){
        e.preventDefault();
        console.log(e);
        return false;
      });
      elem.bind('dragover', function(e){
        e.preventDefault();
      });
    }
  };
});

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Focus on all of the individual items except for the one that was just selected

Within the function thirdNavFunction, I am seeking a way to specifically target all instances of the .third-nav classes, excluding the one that has been clicked on. This must be accomplished without utilizing jQuery. How can I achieve this? var thirdNav = ...

Can you explain the functionality of this Observable in the context of this Angular 2 example?

I'm not too familiar with JavaScript/TypeScript and I have a question about how this code snippet works: onGet() { this.serverService.getServers() .subscribe( (servers: any[]) => this.servers = servers, // an array of anythin ...

Select the Best jQuery Package

Having a variety of packages available for selection. <div class="image-grid-item" data-search="select"> <input name="pack1" type="checkbox" style="display: none;"> </div> <div class="image-grid-item" data-search="select"> <inp ...

How to retrieve properties of the final item in an array using Vue.js

Struggling with Vue.js JavaScript implementation. This is the current code: <div id="app"> <h1>Items</h1> <div v-for="item in items"> <input v-model="item.val"> </div> <button @click="addItem"> Ne ...

Instructions for incorporating highcharts sub modules into a React application

I have been utilizing the react-jsx-highcharts library to seamlessly integrate Highcharts into my React application. Everything is functioning perfectly. However, I am now interested in incorporating the boost module. I attempted to add it by simply using ...

The process of deploying production-ready code using webpack and angular2

Currently, my Angular 2 code utilizes both webpack and grunt. During development, I rely on webpack-dev-server as a grunt task. When it comes to preparing the code for production deployment, I handle all minification tasks through Grunt which results in t ...

Having trouble getting Angular-UI-Select to work with a basic variable on $scope?

How can you reset an array with selected values so that the values can be reselected from the dropdown? I am working with a people array where the values are initially displayed in a select dropdown. When I choose certain names, they get transferred to th ...

What is the process of uploading files using Ajax?

Can anyone help me with submitting a form using ajax that contains images? I have this code snippet which logs an object called "FormData" in the console, but I'm not sure how to properly use it or retrieve it. Is this the correct approach? $("#form_ ...

Angular foreach method encounters a syntax issue

When I use the getTotal.getValues() function to make a server call that returns values like "one", "two", "three" up to "nine", I am able to print them using console.log(res). However, I am facing an issue where I cannot push these returned values into t ...

Node is not functioning properly with Discord.js as expected

Having some trouble catching errors in my code. I'm seeing a red line and an expression expected error behind the period after the catch command. Any suggestions? client.on('message', message => { let args = message.content.subs ...

Getting started with jquery plugins: installation and usage

I have some questions as a beginner. Firstly, I'm interested in obtaining this plugin: Unfortunately, I am having difficulty downloading it and seem to be stuck in a loop. Can someone guide me on the exact steps to download it? Secondly, how do I go ...

What are the steps to implement routing with angular-ui-router?

Recently delving into angularjs, I decided to create a sample app with custom directives. I then attempted to add routing, but unfortunately, it's not functioning properly. Upon starting the project, nothing is being displayed in the browser. Here is ...

Integrating draw2d into Mean.js: A Step-by-Step Guide

I'm new to the MEAN Stack and I'm having trouble with something that seems pretty basic. My issue is trying to inject a new instance of draw2d from their downloadable library. The error message I keep receiving is: "Uncaught Error: [$injector: ...

Sending both the minimum and maximum slider values to PHP using POST can be achieved by formatting the data correctly

I am attempting to transmit the minimum and maximum values to PHP using submit through a dual slider bar The static page makes a request to the server-side PHP code <?php include('server/server.php') ?> Below is the JavaScript code ...

Combining three.js geometry and selecting objects with an octree

I have been utilizing Three.js to showcase a variety of custom geometries in different positions and rotations. These geometries are static and do not change frequently, but users have the ability to manipulate them by adding, deleting, or altering the sha ...

Implementing sound playback within an AJAX response

Recently, I implemented a jQuery code to automatically refresh a specific div. This auto-refresh feature uses AJAX to generate notifications whenever there is a new request from a client, similar to social network notifications. I even incorporated music f ...

Troubleshooting issue with TypeScript: Union types not functioning correctly when mapping object values

When it comes to mapping object values with all primitive types, the process is quite straightforward: type ObjectOf<T> = { [k: string]: T }; type MapObj<Obj extends ObjectOf<any>> = { [K in keyof Obj]: Obj[K] extends string ? Obj[K] : ...

"Limitation observed - function operates only on first attempt

I have been working on a map project that involves pulling information from Google about various locations using the library available at https://github.com/peledies/google-places In order to troubleshoot and identify the issue with the code related to ma ...

Comprehending the Syntax of the ExtJS Framework

I have recently inherited a project utilizing Sencha's ExtJS framework without any handover or documentation. As I navigate through the code, I find myself trying to decipher certain syntax and exploring resources tailored for newcomers to this specif ...

Jade: Incorporating a JavaScript file at a specific location

Is there a way to include a file that is specifically used when rendering a jade file on the client side? Here is an example of the .jade file: button(type="button")#start.flex p#timer.flex button(type="button" )#stop.flex script(src='../public/js/ti ...