I have HTML page that presents user and password, and on load of the page i want it to sent GET request to my local python code with vars and my function should return the downloadable file.
This is my code:
<!DOCTYPE html>
<html>
<body>
<p style="font-family:verdana"> <strong>Username:</strong> <span class="js-content">{{user}}</span> </p>
<button class="js-copy">Copy</button>
<hr>
<p style="font-family:verdana"><strong>Password:</strong> <span class="js-content">{{pass}}</span> </p>
<button class="js-copy">Copy</button>
<input type=hidden name="randomVal" value="{{ randomVal }}">
<input type=hidden name="output" value="{{output}}">
<script>
(function(){
/* Creating textarea only once, but not each time */
let area = document.createElement('textarea');
document.body.appendChild( area );
area.style.display = "none";
let content = document.querySelectorAll('.js-content');
let copy = document.querySelectorAll('.js-copy');
for( let i = 0; i < copy.length; i++ ){
copy[i].addEventListener('click', function(){
area.style.display = "block";
/* because the classes are paired, we can use the [i] index from the clicked button,
to get the required text block */
area.value = content[i].innerText;
area.select();
document.execCommand('copy');
area.style.display = "none";
/* decorative part */
this.innerHTML = '<span style="color: red;">Copied</span>';
/* arrow function doesn't modify 'this', here it's still the clicked button */
setTimeout( () => this.innerHTML = "Copy", 2000 );
});
}
})();
</script>
</body>
</html>
Now how can i make that before it loads \ on load it sends a get request like :
<form method="get" action="/pythonRouteName" style="display: inline;">
(Form will not work for me as it redirects.) And sending this values:
<input type=hidden name="randomVal" value="{{ randomVal }}">
<input type=hidden name="output" value="{{output}}">
With the get request but without moving \ redirecting to this get request.
I want to stay on the same page that shows user and password,
There are a lot of sites that does this - you enter and automatically it download some file but you stay on the same page.
EDIT: Things i tried- added this function :
function startDownload("download_function") {
window.location.href = "download_function";
<input type=hidden name="randomVal" value="{{ randomVal}}">
<input type=hidden name="output" value="{{output}}">>
}
window.onload = startDownload();
</script>
It doesn't do anything. i keep looking for a solution