javascript - 使用js下载,调用函数dispatchEvent chrome给出警告
大家讲道理
大家讲道理 2017-04-11 12:12:28
[JavaScript讨论组]

网上招了一个js下载的代码:

/**
 * Javascript 多文件下载
 * @author Barret Lee
 * @email  barret.china@gmail.com
 */
var downHelper = (function(files){
    var h5Down = !/Trident|MSIE/.test(navigator.userAgent);
    /**
     * 在支持 download 属性的情况下使用该方法进行单个文件下载
     * 目前 FF 还不支持 download 属性,所以 FF 必须另觅他法!
     * @param  {String} fileName
     * @param  {String|FileObject} contentOrPath
     * @return {Null}
     */
    function downloadFile(fileName, contentOrPath){
        var aLink = document.createElement("a"),
            evt = document.createEvent("HTMLEvents"),
            isData = contentOrPath.slice(0, 5) === "data:",
            isPath = contentOrPath.lastIndexOf(".") > -1;

        // 初始化点击事件
        // 注:initEvent 不加后两个参数在FF下会报错
        evt.initEvent("click",false,false);

        // 添加文件下载名
        aLink.download = fileName;

        // 如果是 path 或者 dataURL 直接赋值
        // 如果是 file 或者其他内容,使用 Blob 转换
        aLink.href = isPath || isData ? contentOrPath
            : URL.createObjectURL(new Blob([contentOrPath]));

        aLink.dispatchEvent(evt);
    }

    /**
     * [IEdownloadFile description]
     * @param  {String} fileName
     * @param  {String|FileObject} contentOrPath
     */
    function IEdownloadFile(fileName, contentOrPath, bool){
        window.open(contentOrPath);
    }

    /**
     * [parseURL description]
     * @param  {String} str [description]
     * @return {String}     [description]
     */
    function parseURL(str){
        return str.lastIndexOf("/") > -1 ? str.slice(str.lastIndexOf("/") + 1) : str;
    }

    return function(files){
        // 选择下载函数
        var downer = h5Down ? downloadFile : IEdownloadFile;

        // 判断类型,处理下载文件名
        if(files instanceof Array) {
            for(var i = 0, l = files.length; i < l ; i++)
                // bug 处理
                downer(parseURL(files[i]), files[i], true);
        } else if(typeof files === "string") {
            downer(parseURL(files), files);
        } else {
            // 对象
            for(var file in files) downer(file, files[file]);
        }
    }
})();

但是调用的时候,chrome给出了警告,A DOM event generated from JavaScript has triggered a default action inside the browser. This behavior is non-standard and will be removed in M53, around September 2016. See https://www.chromestatus.com/features/5718803933560832 for more details.,我看了一下警告链接给出的说明:

According to the UI Events specification un-trusted events (i.e. those created by JavaScript) should not invoke the default action. 'click' is the only event that is a legacy permitted case.

The isTrusted support was added in https://www.chromestatus.com/... which identifies trusted events from un-trusted events.

We wish to prevent synthetic events from executing the default action, aligning with Firefox and IE.

上面代码中自定义事件,就是click类型啊,应该是一个合法的事件啊,浏览器会什么会警告呢?百思不得其解。

大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

全部回复(1)
黄舟

请问楼主解决了吗?
chromium: [INFO:CONSOLE(1)] "A DOM event generated from JavaScript has triggered a default action inside the browser. This behavior is non-standard and will be removed in M53, around September 2016. See https://www.chromestatus.com/... for more details.", source: https://as.alipayobjects.com/... (1)
我也遇到了。。

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号