简体   繁体   中英

Make a download after call a javascript function

I want to do this, I have a tag with a href attr that in normal sense would download a file, but I want to make it downloaded after change some info in the web page look at this code:

This is the html where I have the html and where the user can download a file.

   <li class="imprimir"><h2><a href="javascript:descargarNoticia()" id="descargar"  >Descargar Noticia</a></h2></li>

And here the JS that would change information and then download the file but I cant do that.

function descargarNoticia(){
$.post("http://www.xxx.com/noticia/datosUsuarios",
   function(data){
    $("#empresa").show();
    $("#email").html(data['email'])
    $("#telefono").html(data['telefono'])
    $("#direccion").html(data['direccion'])
    $("#empresa").html(data['empresa'])

    var words = $('#wrapper-960').text().split(' ').length;
    var alto=words*0.264583333;
   $("#descargar").attr("href","http://pdfcrowd.com/url_to_pdf/?width=500mm&height="+alto+"mm")
            }
           },'json');
};

Thanks.

If you have the address of the file, just open that address:

window.open("http://...");

EDIT: Damn, ctrl-alt-dileep beat me to it.

Can you not just go to that URL after changing it?

var URL= "http://pdfcrowd.com/url_to_pdf/?width=500mm&height="+alto+"mm";
$("#descargar").attr("href",URL)
window.location = URL;

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