zoukankan      html  css  js  c++  java
  • JS 下载 弹框被拦截解决方案

    JS 下载 弹框被拦截解决方案

    字数311 阅读0 评论0 

    总结

    1. 早期下载的简单用法 : window.open(downloadUrl)
    2. 下载弹框会被拦截, 解决方案是使用 form 来实现
      //Jquery 版, 多次下载防止,添加多个 form if ($('#_downloadWin').length > 0) { $('#_downloadWin').attr('action', url); $('#_downloadWin input').val(path);} else { // 传参 则在 form 里面 添加 隐藏域(<input type="hidden"/>) $('body').append($(`<form id="_downloadWin" action="${url}" target="_blank" method="get"><input name="path" type="hidden" value="${path}" /></form>`)); } $('#_downloadWin').submit();
      //JavaScript版 var f = document.createElement("form"); document.body.appendChild(f); var i = document.createElement("input"); i.type = "hidden"; f.appendChild(i); i.value = "5"; i.name = "price"; f.action = "aa.asp"; //下载的url 地址 f.submit();

    问题描述与解决方案

    早期下载文件的时候,是直接使用 window.open(downloadUrl) 这种简单粗暴的方法来实现的.

    但是到目前(2016.05.17), window.open 已经被大部分的主流浏览器给拦截了, 如下图, 需要人工在点击一次允许打开链接. 这样大大的降低了 友好的交互.


    Paste_Image.png

    于是需要寻找新的解决方案, 就使用 form 表单元素来进行下载, 注意 Form 表单的 target 要设置为 _blank* 在新的窗口打开,这样就不会影响本页面

  • 相关阅读:
    Qt中使用cout, cin, cerr
    linux下清理系统垃圾
    linux清理内存命令
    boost 特点
    linux boost 安装
    valgrind 的使用及错误信息分析
    ArcGIS Engine 编辑介绍
    ArcGIS Engine 编辑- IWorkspaceEdit
    ArcGIS Engine 编辑- ITask
    CreateFeature与CreateFeatureBuffer区别
  • 原文地址:https://www.cnblogs.com/zhongxia/p/5500974.html
Copyright © 2011-2022 走看看