KNIGHT MAHAJAN
KNIGHT MAHAJAN

Reputation: 491

Download a pdf automatically when a webpage is opened

I want that as soon as my .html file is opened , a pdf starts to download automatically (support for pc , tablets, phones) . I am not sure what am I doing wrong ? Thank you . Any sure short JavaScript is more than welcome

<a href = "/public/news.pdf" download> </a>

Upvotes: 0

Views: 4531

Answers (4)

Amulya Koppula
Amulya Koppula

Reputation: 174

Try this:

<html>
<body onload="myFunction()">
    <script>
        function myFunction() {
            document.getElementById('a').click();
        }
    </script>
    <a href="./image.pdf" download="image" id="a"> <img     src="./image.pdf">
    </a>
</body>
</html>

Upvotes: 0

vivek manavadariya
vivek manavadariya

Reputation: 230

$(document).ready(function() {
    var downloadUrl = "your_file_url";
    setTimeout("window.location.assign('" + downloadUrl + "');", 1000);
});

using javascript

<iframe id="download_iframe" style="display:none;"></iframe>
<script>
function Download() {
    document.getElementById('download_iframe').src = "//somewhere.abc/somefolder/something.exe";
};
Download();
</script>

This way, your client will be asked whether to "Save" or "Run" the file.

Upvotes: 0

Bachcha Singh
Bachcha Singh

Reputation: 3934

Use this:

HTML

<a id="downloadLink" href="news.pdf" download></a>

Java Script

Solution 1 :

<script> 
    window.onload = function() {
        document.getElementById('downloadLink').click();
    }
</script>

Solution 2: after 2 second

<script> 
    var downloadStartTime = setTimeout(function () {
       document.getElementById('downloadLink').click();
    }, 2000);
</script>

Upvotes: 3

Lalit Mohan
Lalit Mohan

Reputation: 474

window.onload function is that you are looking for.

window.onload = function() {
  var url = "Your file url";
  var w=window.open(url, '_blank');
  w.focus();
};

This will help you

Upvotes: 3

Related Questions