JS event error to force and rename download file

0

Hello, I have a Script to force download via remote URL that I got as an answer here on the site, however I think I must have done something wrong because the Download event is not starting and not renaming the file at the time of download.

Update: With the help of Dontvote the script is downloading the case only need to rename the file of the Remote Url of rest is ok, someone could help me with this.

Note I changed the code in the image file question to video file.

 <!DOCTYPE html>
<html>
<body>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script><style>.hide-link{position:absolute;top:0px;left:0px;}</style><script>functiondownload(url,nome){varel=document.createElement("a");
        el.download = nome; //Define o nome
        el.href = url; //Define a url
        el.target = "_blank"; //Força abrir em uma nova janela
        el.className = "hide-link"; //Adiciona uma classe css pra ocultar

    document.body.appendChild(el);

    if (el.fireEvent) {
        el.fireEvent('onclick');//Simula o click pra navegadores com suporte ao fireEvent
    } else {
        //Simula o click
        var evObj;

        evObj = document.createEvent("MouseEvents");
        evObj.initEvent("click", true, false);
        el.dispatchEvent(evObj);
    }

    //Remove o link da página
    setTimeout(function() { document.body.removeChild(el); }, 100);
}
</script>
<div class="hide-link" onload="download('http://thumb.mais.uol.com.br/14317945.mp4', 'video01.mp4')">Clique aqui</div>  
</body>    
</html>
    
asked by anonymous 03.09.2015 / 15:56

1 answer

1

You're giving error at this point:

evObj.initEvent(type, true, false);

Because the type variable is not defined. I tried putting "click" hard-code and it worked:

evObj.initEvent("click", true, false);

Fiddle

As I said, the div element does not have the event onload . When you posted the code using a button with onclick , the function worked.

    
03.09.2015 / 15:58