简体   繁体   English

Javascript/JQuery 放置事件未在 Internet Explorer IE 11 中触发

[英]Javascript/JQuery drop event not firing in Internet Explorer IE 11

I'm dragging and dropping images from a different browser tab into the tab for my web page.我正在将图像从不同的浏览器选项卡拖放到我的网页的选项卡中。 My event handlers for the "drop" event are working in every other desktop browser except Internet Explorer 11.我的“drop”事件的事件处理程序在除 Internet Explorer 11 之外的所有其他桌面浏览器中都有效。

IE just navigates away to the URL of the image I dropped, rather than firing the "drop" event and letting my JS code do what it wants with it (as happens in Chrome, Firefox, Opera and Safari, on Windows 7). IE 只是导航到我放置的图像的 URL,而不是触发“放置”事件并让我的 JS 代码用它做它想做的事(就像在 Windows 7 上的 Chrome、Firefox、Opera 和 Safari 中发生的那样)。 Code is below.代码如下。 Note none of the alerts listed in the code fire.请注意,代码中列出的警报均未触发。

I even followed the advice given on Microsoft's page here:https://msdn.microsoft.com/en-us/library/ms536929(v=vs.85).aspx regarding cancelling the default action of "dragenter" and "specifying window.event.returnValue=false in both the ondragenter and ondragover event handlers" (note: other browsers didn't require me to have a dragenter event handler)我什至遵循了 Microsoft 页面上给出的建议:https ://msdn.microsoft.com/en-us/library/ms536929(v=vs.85).aspx 关于取消“dragenter”和“specifying window.event.returnValue=false ”的默认操作window.event.returnValue=falseondragenterondragover事件处理程序中”(注意:其他浏览器不要求我有 dragenter 事件处理程序)

$(window).on("dragenter", function(event) {
    alert('drag enter');
    event.returnValue = false;
    event.preventDefault();  
    event.stopPropagation();
});

$(window).on("dragover", function(event) {
    alert('drag over');
    event.returnValue = false;
    event.preventDefault();  
    event.stopPropagation();
});

$(window).on("dragleave", function(event) {
    alert('drag leave');
    event.preventDefault();  
    event.stopPropagation();
});

$(window).on("drop", function(event) {
    alert('drop');
    event.preventDefault();  
    event.stopPropagation();
    var imageSrc = $(event.originalEvent.dataTransfer.getData('text/html'))
            .filter(function(i, elm){return $(elm).is('img');}).attr('src');

// Now do something with the imageSrc URL:

});

Many thanks for any suggestions!非常感谢您的任何建议!

It is working fine in IE Browser Version:11.0.40 for jQuery 2.2.4 version.
Please check your jQuery version    

Note: for me event has been fired for two times when dragging image from desktop to IE 11 browser.

Please find Demo link.请找到演示链接。

Edit: Interesting what I've got here (SO question/answer) , they had a similar problem dragging between two Internet Explorer 11 windows, on the same domain.编辑:有趣的是我在这里得到了什么(所以问题/答案) ,他们在同一域的两个 Internet Explorer 11 窗口之间拖动时遇到了类似的问题。 So one more reason, if they are from different domains.所以还有一个原因,如果它们来自不同的域。 He quotes:他引用:

so far I understood that this will work on Windows 10 MS browsers with at least 1607 as version到目前为止,我知道这将适用于版本至少为 1607 的 Windows 10 MS 浏览器


First, this is by no means intended to be an answer to this question, it merely serves as a group of points that may help build a final answer to this puzzling problem首先,这绝不是要回答这个问题,它只是作为一组可能有助于为这个令人费解的问题建立最终答案的要点

Two cases scenario两种情况

  1. On the same domain在同一个域

    Tested when both pages are on localhost , Those events has fired normally: you will have to change getData('text/html') to getData('text') becauseIE support only that, 'URL' orfiles so you need to setsetData() from the original page accordingly当两个页面都在localhost上进行测试时,这些事件已正常触发:您必须将getData('text/html')更改为getData('text')因为IE 仅支持'URL'文件,因此您需要设置相应地从原始页面设置数据setData()
    (Generally, if the dragged markup is an image without any links, you're fine the getData('text') gives you the attribute src of the image) (通常,如果拖动的标记是没有任何链接的图像,则getData('text')会为您提供图像的属性src就可以了)

  2. On different domains在不同的领域

    Here is the part where this is not much of an answer, the following points have been tried and are given here to be retested, tweaked or expanded in order to find a solution.这是这不是一个很好的答案的部分,已经尝试了以下几点并在此处给出以供重新测试,调整或扩展以找到解决方案。 As a final thought that I'm putting here first: may be this is not possible, because as you may already know, dragged markup can have inline scripts, making the target vulnerable to XSS attacks.作为最后一个想法,我首先把它放在这里:可能这是不可能的,因为你可能已经知道,拖动的标记可以有内联脚本,使目标容易受到 XSS 攻击。 It's not very unlikely that hackers have tried to make it happen (roughly as I'm doing right know) and each time Microsoft has counteract it.黑客试图让它发生(大致就像我做的那样)并且每次微软都抵消它并不是不太可能。

    • As the original poster pointed out the use of returnValue=false is pointless.正如原始海报所指出的,使用returnValue=false是毫无意义的。 I've tried it on the original event event.originalEvent and with event as a window 's event and as the handler parameter.我已经在原始事件event.originalEvent上尝试过它,并将event作为window的事件和handler参数。

    • You may think since I've mention domain that this is a cross domain issue (very legitimate) here's what I've tried in PHP:您可能会认为,因为我已经提到了域,所以这是一个跨域问题(非常合法),这是我在 PHP 中尝试过的:

    header("Access-Control-Allow-Origin: *");

    • After IE had been known for been vulnerable to XSS attacks, it may have taken drastic measures against it in IE 11, so reverting to a previous version's behaviour of it, IE9 for instance:在 IE 因容易受到 XSS 攻击而为人所知之后,它可能在 IE 11 中对其采取了严厉的措施,因此恢复到以前版本的行为,例如 IE9:

    header('X-UA-Compatible: IE=EmulateIE9');
    header('X-UA-Compatible: IE=9');

NB The following point is not intended to be a viable solution (at least not from a developer's perspective) it is an attempt to narrow down the possibilities of the origin of the problem注意以下几点并不是一个可行的解决方案(至少不是从开发人员的角度来看)它是试图缩小问题起源的可能性

  • You may want to try Internet Explorer's:您可能想尝试 Internet Explorer 的:

    internet options>Custom level...-->miscellaneous--> under the label 'allow the dragging of content between separate windows' --> check enable

Or Internet Explorer security zones registry entries for advanced users or using this reference高级用户的 Internet Explorer 安全区域注册表项或使用此参考

Notice that for the purpose of testing you can make cross domain dragging between IE and Firefox/Chrome back and forth with DataTransfer behaving roughly as between two IEs on the same domain.请注意,出于测试目的,您可以在 IE 和 Firefox/Chrome 之间来回跨域拖动, DataTransfer的行为大致与同一域中两个 IE 之间的行为相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM