0% found this document useful (0 votes)
74 views

Open Random PDF File From Folder With Coding (HTML Main Code Used) - Stack Overflow

The document is a question posted on Stack Overflow asking how to open a random PDF file from a folder using HTML coding. The question details that the website is coded in HTML and contains PDF recipe files numbered 1-15. The top answer provides a JavaScript code snippet that generates a random number between 1-15, sets the source of an embed element to the random number plus .pdf, and appends it to the body on page load.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views

Open Random PDF File From Folder With Coding (HTML Main Code Used) - Stack Overflow

The document is a question posted on Stack Overflow asking how to open a random PDF file from a folder using HTML coding. The question details that the website is coded in HTML and contains PDF recipe files numbered 1-15. The top answer provides a JavaScript code snippet that generates a random number between 1-15, sets the source of an embed element to the random number plus .pdf, and appends it to the body on page load.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

04/01/23, 12:19 open random pdf file from folder with coding (HTML main code used) - Stack

TML main code used) - Stack Overflow

open random pdf file from folder with coding (HTML main code
used)
Asked 2 years, 4 months ago Modified 2 years, 4 months ago Viewed 194 times

I have made this website where I collect recipes I can eat.

0 I would like to add a 'random' feature, where one of the recipes opens and I don't have to
chose what to eat.

My knowlege of coding is limit (like a year of highschool making a HTML website limited) but I
do now it has to be possible. I also learned very briefly about a random number generator
option in PHP and Javascript.

The website is coded with HTML and Notepad++.

The files are all pdf typed and like this, '1.pdf' '2.pdf'

html file pdf random randomaccessfile

Share Improve this question Follow asked Aug 12, 2020 at 11:53
janneke
1

1 Welcome to Stack Overflow! Please visit the help center, take the tour to see what and How to Ask. Do
some research, search for related topics on SO; if you get stuck, post a minimal reproducible example of
your attempt, noting input and expected output using the [<>] snippet editor. – mplungjan Aug 12,
2020 at 11:57

Sorted by:
1 Answer
Highest score (default)

You mean something like

0 <script>
const max = 15; // the largest number in your PDF filename
const min = 1;
const rnd = Math.floor(Math.random() * (max - min + 1) + min);
const pdf = document.createElement("embed");
pdf.width="800px"; // or what you want
pdf.height="2100px";
pdf.src=rnd+".pdf"
window.addEventListener("load",function() {
document.body.appendChild(pdf);
});
</script>

https://stackoverflow.com/questions/63375985/open-random-pdf-file-from-folder-with-coding-html-main-code-used 1/2
04/01/23, 12:19 open random pdf file from folder with coding (HTML main code used) - Stack Overflow

Share Improve this answer Follow answered Aug 12, 2020 at 12:03
mplungjan
163k 27 173 231

https://stackoverflow.com/questions/63375985/open-random-pdf-file-from-folder-with-coding-html-main-code-used 2/2

You might also like