Download a pdf automatically when a webpage is opened - javascript

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>

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

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>

$(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.

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>

Related

jQuery can't refresh page with parameters

I'm sorry to post with my question but I really don't understand. There are some moments like this one where I have the impression that nothing work in web.
I'm trying to do something pretty simple, I just want to reload a page and add a parameter by clicking on a button with jQuery. I searched and of course I found solutions but it doesn't work for me and I really don't understand why.
I tried several solutions but none works.
$(document).ready(function() {
$("#btnDL").click(function() {
/*window.location.href = window.location.href.replace( /[\?#].*|$/, "?dl=1" );*/
/*if(!window.location.hash) {
window.location.replace(window.location.href + "?dl=1");
}*/
/*var url = new URL(window.location.href);
url.searchParams.set('dl','1');
location.reload();*/
var url = window.location.href;
url += '?dl=1'
window.location.href = url;
});
});
Here is my code (I let some solutions I tried in comment), this doesn't work, when I click on my button it just refresh the page but the URL is still the same.
I think you've seen this question a lot of times so sorry for the inconvenience.
Thank you
$(document).ready(function() {
$("#btnDL").click(function() {
var url = window.location.href;
window.location.href = window.location.href + "?dl=1";
});
});
try this
I have tested it on Google Chrome and it is appending id with the url. Try it:
<html>
<head>
<script
src="https://code.jquery.com/jquery-3.3.1.js"
integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60="
crossorigin="anonymous"></script>
<script>
$(document).ready(function() {
$("#btnDL").click(function() {
var url = window.location.href;
alert(url);
url += '?dl=1'
window.location.href = url;
})
});
</script>
</head>
<body>
<button id='btnDL'>
Click Me
</button>
</body>
</html>

Open a new window from the apps script add-on menu

I have an add-on where I need to redirect to external URL instead loading with Dialog or Sidebar when the menu is clicked.
I have the sidebar code
function showContactDialog() {
var html = HtmlService.createTemplateFromFile('Website').evaluate().setSandboxMode(HtmlService.SandboxMode.IFRAME).setWidth(480).setHeight(380);
SpreadsheetApp.getUi().showModalDialog(html, 'Website');
}
In your HTML you can open a new tab (if your browser supports such a thing) but you can't redirect the page.
In your HTML, you can do something like below:
<script>
function openPage() {
window.open('https://google.com', '_blank');
}
window.onload = openPage;
</script>
In your Website.html place this code
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<script type="text/javascript">
function opentab(){
window.open(LINK_YOU_WANT_TO_OPEN, '_blank');
}
</script>
</head>
<body onload="opentab()">
</body>
</html>
This works fine for me
function open_new_doc()
{
var html = '<script>google.script.host.close();'+
'window.location.href = "'+open_url+'"</script>';
var html_open = HtmlService.createHtmlOutput(html).setSandboxMode(HtmlService.SandboxMode.IFRAME);
DocumentApp.getUi().showModalDialog(html_open, 'Opening New Window...');
}

How to create retry connecting button? HTML/JS

The question is how to create a button that retries connection to a website. The non working is:
Retry/Refresh
try window.location.reload(true)
documentation
The working code should be:
<button onclick="retryConnect()">Retry</button>
<script>
function retryConnect(){
location="retry.html";
}
</script>
Create a file called retry.html in the same directory, and write in it:
<p onload="loadPage()">Please wait...</p>
<script>
var delayLoad;
function loadPage() {
delayLoad = setTimeout(showPage, 1000);
}
function showPage() {
location="the previous page";
}
</script>

Run Function in original page from window.open javascript

how can I run Function in original page from window.open
original_page.html
<div id="Processing" style="display:none;">
<button onclick="window.open("Processing_window.html")">open</button>
</div>
<script language="JavaScript">
function submit_form() {
var upgradeForm = document.getElementById('upgradeForm');
setTimeout("upgradeForm.submit()",3000);
}
</script>
========
Processing_window.html
<script language="JavaScript">
function Processing_window() {
var doc = window.opener.document, Processing = doc.getElementById("Processing");
Processing.style = '';
submit_form(); //Here the problem
window.close();
}
setTimeout ("Processing_window()",5000);
</script>
========
I went to run "submit_form();" funcion from Processing_window.html
To access functions from the opener, use window.opener.functionName, assuming both are in the same domain.
function Processing_window() {
var doc = window.opener.document, Processing = doc.getElementById("Processing");
Processing.style = '';
// Here's the solution
window.opener.submit_form();
window.close();
}
setTimeout ("Processing_window()",5000);
You need to HTML encode the quotation marks in the code in the attribute, or use apostrophes:
<button onclick="window.open('Processing_window.html')">open</button>
As long as the page that you open has the same origin (same server, same protocol), you can use the opener property to access the parent windows window object. There's where you find the reference to the function:
window.opener.submit_form();
You can, but not cross-domain. Only in the same domain.
<script>
function new_win_fun() {
var wind = window.open("Processing_window.html");
var upgradeForm = wind.document.getElementById("upgradeForm");
wind.setTimeout("upgradeForm.submit()",3000);
}
</script>
<button onclick="new_win_func()">Run</button>

Use JavaScript variable as iframe src?

I'm trying to use a JavaScript variable as the src in a <iframe>, the user will then be able to load other content by choosing scr in a menu, which changes the variable. the default src URL is apps/start.php.
Here some code from my index.php:
<script>
var appurl = "start.php";
document.getElementById("app").src = "apps/" + appurl();
</script>
<iframe id="app" src="">
</iframe>
The problem is that i can't get this first small part working... The <script> tag is in the <head>. And when the src is changing will the new document automatically load?
Wait for the page to load (note that this is only going to work when the iframe is on your domain)
window.onload = function(){
var appurl = "start.php";
document.getElementById("app").src = "apps/" + appurl;
};
The problem is that the Javascript is run before the iframe is there. Put your javascript below your iFrame or inside the document ready and it will work.
Example document onload:
<script>
window.onload = function ()
{
var appurl = "start.php";
document.getElementById("app").src = "apps/" + appurl();
}
</script>
<iframe id="app" src="">
</iframe>
<script>
window.onload = function ()
{
var appurl = "start.php";
document.getElementById("app").src = "apps/" + appurl;
}
</script>
<iframe id="app" src="">
</iframe>
Try
<script>
window.onload = function(){
var appurl = 'start.php';
document.getElementById("app").src = "apps/" + appurl;
};
</script>

Categories