Skip to content Skip to sidebar Skip to footer

Drag And Drop: How To Get The Url Of Image Being Dropped If Image Is A Link (not The Url Of The Link)

I have this code: function drop(evt) { evt.stopPropagation(); evt.preventDefault(); var imageUrl = evt.dataTransfer.getData('URL'); alert(imageUrl); } FIDDLE If y

Solution 1:

Using jQuery. (I'm just using it to get the "src" attribute so i think you can do without it)

I replaced:

var imageUrl = evt.dataTransfer.getData('URL');

with:

var imageUrl = evt.dataTransfer.getData('text/html');

Oh, and I replace the alert with a console.log so you'll need to open it.

Solution 2:

Here's how I'd do it (only tested in Firefox):

  • use jQuery
  • analyze the dropped html-code using jQuery: search for image inside with .find()
  • read src-attribute from the found image with .attr()

Here's the important part from my new drop(event) function:

// Get text/html instead of text/uri-list!// So you get raw html that is passed even between different websitesvar droppedHTML = evt.dataTransfer.getData("text/html");

// add this html to some container.// if you skip this, the following code won't work if a single img element is droppedvar dropContext = $('<div>').append(droppedHTML);

// now you can read the img-url (not link-url!!) like this:var imgURL = $(dropContext).find("img").attr('src');

Solution 3:

functiondrop(evt) {
    evt.stopPropagation();
    evt.preventDefault(); 
    var imageUrl = evt.dataTransfer.getData('text/html');
    var rex = /src="?([^"\s]+)"?\s*/;
    var url, res;
    url = rex.exec(imageUrl);
    alert(url[1]);
}

It works on Chrome and Firefox, but note that if you drag an image from Chrome to Firefox it doesn't work.

Solution 4:

Here is my fiddle: http://fiddle.jshell.net/ep2z5/

This works in chrome on my Mac. Basically you take the html returned, and then convert into jquery object, and since the return string is a meta tag, and an img tag, you have to take the 2nd element (number 1 in the array returned by jquery parsing) and jquery-ify that item, and then grab the src attribute.

Update: checked firefox and it isn't working there, researching now.

Update: Okay here is the newest fiddle tested in both chrome and firefox. http://fiddle.jshell.net/ep2z5/6/

It uses recursion, which can be dangerous, but i think will work okay, as long as there isn't to many nested elements.

Solution 5:

<body><!--the mouse over and dragging class are defined on each item--><divid="root"style="margin:20px 0 0 10px;"><divid="handle"style="float:left;margin:2px 0 0 10px; padding:5px; background-color:#FFCC00;">Drage Folder</div><divid="drop"style="float:left;margin:2px 0 0 110px; padding:5px; background-color:#CCCC00;">Drop Folder</div></div><imgid="example"src="fold.jpg"style="position: relative" /><scripttype="text/javascript">Drag.init(document.getElementById("example"));
</script>

Create drag-drop.js file and add following code.

varDrag = {
    obj : null,

    init : function(o, oRoot, minX, maxX, minY, maxY, bSwapHorzRef, bSwapVertRef, fXMapper, fYMapper)
    {
            o.onmousedown     = Drag.start;

            o.hmode                                  = bSwapHorzRef ? false : true ;
            o.vmode                                  = bSwapVertRef ? false : true ;

            o.root = oRoot && oRoot != null ? oRoot : o ;

            if (o.hmode  && isNaN(parseInt(o.root.style.left  ))) o.root.style.left   = "0px";
            if (o.vmode  && isNaN(parseInt(o.root.style.top   ))) o.root.style.top    = "0px";
            if (!o.hmode && isNaN(parseInt(o.root.style.right ))) o.root.style.right  = "0px";
            if (!o.vmode && isNaN(parseInt(o.root.style.bottom))) o.root.style.bottom = "0px";

            o.minX     = typeof minX != 'undefined' ? minX : null;
            o.minY     = typeof minY != 'undefined' ? minY : null;
            o.maxX    = typeof maxX != 'undefined' ? maxX : null;
            o.maxY    = typeof maxY != 'undefined' ? maxY : null;

            o.xMapper = fXMapper ? fXMapper : null;
            o.yMapper = fYMapper ? fYMapper : null;

            o.root.onDragStart  = newFunction();
            o.root.onDragEnd    = newFunction();
            o.root.onDrag                          = newFunction();
    },

    start : function(e)
    {
            var o = Drag.obj = this;
            e = Drag.fixE(e);
            var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);
            var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
            o.root.onDragStart(x, y);

            o.lastMouseX          = e.clientX;
            o.lastMouseY          = e.clientY;

            if (o.hmode) {
                    if (o.minX != null)      o.minMouseX           = e.clientX - x + o.minX;
                    if (o.maxX != null)    o.maxMouseX         = o.minMouseX + o.maxX - o.minX;
            } else {
                    if (o.minX != null) o.maxMouseX = -o.minX + e.clientX + x;
                    if (o.maxX != null) o.minMouseX = -o.maxX + e.clientX + x;
            }

            if (o.vmode) {
                    if (o.minY != null)     o.minMouseY          = e.clientY - y + o.minY;
                    if (o.maxY != null)    o.maxMouseY         = o.minMouseY + o.maxY - o.minY;
            } else {
                    if (o.minY != null) o.maxMouseY = -o.minY + e.clientY + y;
                    if (o.maxY != null) o.minMouseY = -o.maxY + e.clientY + y;
            }

            document.onmousemove        = Drag.drag;
            document.onmouseup             = Drag.end;

            returnfalse;
    },

    drag : function(e)
    {
            e = Drag.fixE(e);
            var o = Drag.obj;

            var ey      = e.clientY;
            var ex      = e.clientX;
            var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);
            var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
            var nx, ny;

            if (o.minX != null) ex = o.hmode ? Math.max(ex, o.minMouseX) : Math.min(ex, o.maxMouseX);
            if (o.maxX != null) ex = o.hmode ? Math.min(ex, o.maxMouseX) : Math.max(ex, o.minMouseX);
            if (o.minY != null) ey = o.vmode ? Math.max(ey, o.minMouseY) : Math.min(ey, o.maxMouseY);
            if (o.maxY != null) ey = o.vmode ? Math.min(ey, o.maxMouseY) : Math.max(ey, o.minMouseY);

            nx = x + ((ex - o.lastMouseX) * (o.hmode ? 1 : -1));
            ny = y + ((ey - o.lastMouseY) * (o.vmode ? 1 : -1));

            if (o.xMapper)                         nx = o.xMapper(y)
            elseif (o.yMapper)  ny = o.yMapper(x)

            Drag.obj.root.style[o.hmode ? "left" : "right"] = nx + "px";
            Drag.obj.root.style[o.vmode ? "top" : "bottom"] = ny + "px";
            Drag.obj.lastMouseX               = ex;
            Drag.obj.lastMouseY               = ey;

            Drag.obj.root.onDrag(nx, ny);
            returnfalse;
    },

    end : function()
    {
            document.onmousemove = null;
            document.onmouseup   = null;
            Drag.obj.root.onDragEnd(       parseInt(Drag.obj.root.style[Drag.obj.hmode ? "left" : "right"]),
                                                                    parseInt(Drag.obj.root.style[Drag.obj.vmode ? "top" : "bottom"]));
            Drag.obj = null;
    },

    fixE : function(e)
    {
            if (typeof e == 'undefined') e = window.event;
            if (typeof e.layerX == 'undefined') e.layerX = e.offsetX;
            if (typeof e.layerY == 'undefined') e.layerY = e.offsetY;
            return e;
    }
};

Post a Comment for "Drag And Drop: How To Get The Url Of Image Being Dropped If Image Is A Link (not The Url Of The Link)"