简体   繁体   中英

Show popup after link clicked and download a file

I created a jquery modal popup modal of Bootstrap.

I want the popup show after link cliked and download a file.

-- HTML --

<!-- Link -->
<a class="download-link" data-toggle="modal" data-target="#myModal" href="http://ourdomain.com/get/file_download.zip" rel="nofollow">Download</a>

<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        <h4 class="modal-title" id="myModalLabel">Modal title</h4>
      </div>
      <div class="modal-body">
        This is the body text
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
      </div>
    </div>
  </div>
</div>

-- Javascript --

$('[data-toggle=modal]').on('click', function (e) {
    var $target = $($(this).data('target'));
    $target.data('triggered',true);
    setTimeout(function() {
        if ($target.data('triggered')) {
            $target.modal('show').data('triggered',false);
        };
    }, 1000); // milliseconds
    return false;
});

The problem is when I click the link, the file is not downloaded, just show up popup. Any idea how to do it??

You can create another link, hide it and trigger click on this anchor when your modal is opening:

HTML:

<!-- Link -->
<a href="#" data-toggle="modal" data-target="#myModal" rel="nofollow" download>Download</a>
<a href="http://ourdomain.com/get/file_download.zip" id="download" class="hidden">aa</a>

<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        <h4 class="modal-title" id="myModalLabel">Modal title</h4>
      </div>
      <div class="modal-body">
        This is the body text
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
      </div>
    </div>
  </div>
</div>

JS:

$('[data-toggle=modal]').on('click', function(e) {
  var $target = $($(this).data('target'));
  $target.data('triggered', true);
  setTimeout(function() {
    if ($target.data('triggered')) {
      $target.modal('show').data('triggered', false);
    };
  }, 1000); // milliseconds
  return false;
});

$('#myModal').on('show.bs.modal', function () {
  $('#download')[0].click();
});

CODEPEN

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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