You are on page 1of 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