<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/how-to-load-a-random-html-page-inline-with-javascript-lg49qvbvs" -->

---
title: How to Load a Random HTML Page Inline with JavaScript
description: You can show a different HTML page on every visit by picking a random file from an array, loading it with fetch, and setting it as a div's innerHTML. The code...
canonical: https://daily.dev/posts/how-to-load-a-random-html-page-inline-with-javascript-lg49qvbvs
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: How to Load a Random HTML Page Inline with JavaScript | daily.dev
og:description: You can show a different HTML page on every visit by picking a random file from an array, loading it with fetch, and setting it as a div's innerHTML. The code...
og:url: https://daily.dev/posts/how-to-load-a-random-html-page-inline-with-javascript-lg49qvbvs
og:image: https://api.daily.dev/og/posts/lG49QVbvS.png
og:image:alt: How to Load a Random HTML Page Inline with JavaScript
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# How to Load a Random HTML Page Inline with JavaScript

**[Kirupa](https://daily.dev/sources/kirupa)** · 18 min read · 0 upvotes · 0 comments

## Summary

You can show a different HTML page on every visit by picking a random file from an array, loading it with fetch, and setting it as a div's innerHTML. The code checks response.ok so a 404 page doesn't get loaded. Loaded fragments take the parent page's CSS, and a full document's style tag leaks onto the whole page, so DOMParser can grab just the body. Only use innerHTML with trusted HTML, because onerror handlers still run. fetch can't read file:// addresses, so test through a local server like python3 -m http.server.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://www.kirupa.com/html5/loading_random_page_inline_pg1.htm>

## Questions this post answers

### Why does fetch fail when I open my HTML file directly from disk?

Browsers don't let fetch read files through a file:// address, so loading a local HTML file by double-clicking it fails. You need to serve the page through a web server, even a tiny local one. Running python3 -m http.server from the project folder and opening http://localhost:8000 works, as does VS Code's Live Server extension.

_Developers debugging local fetch quirks keep up with web platform gotchas on daily.dev._

### Is it safe to use innerHTML to insert HTML I fetched into a div?

It is safe only when you wrote or trust the HTML. Script tags inserted through innerHTML won't run, but event handler attributes like an image's onerror will, which is how cross-site scripting sneaks in. Use textContent for plain text, sanitize untrusted HTML with DOMPurify, or show someone else's page in an iframe.

_Anyone shipping dynamic HTML insertion can follow XSS and sanitization practices on daily.dev._

### Why do styles from a fetched full HTML page affect my whole page when I use innerHTML?

Setting innerHTML drops the html, head and body tags but keeps style, title and meta elements, so a loaded style tag now applies to the entire page. Loaded HTML is also styled by the parent page's CSS. Keep loaded pages as small fragments, or parse the text with DOMParser and insert only its body's child nodes.

_Developers untangling CSS leakage will find more front-end gotchas on daily.dev._

## Similar posts on daily.dev

- [Display a Random Image with JavaScript or the Unsplash API](https://daily.dev/posts/display-a-random-image-with-javascript-or-the-unsplash-api-whvnnxda2) · Kirupa · 0 upvotes · 0 comments
- [Issue \#768: The things you can now do directly in the browser — Frontend Focus](https://daily.dev/posts/issue-768-the-things-you-can-now-do-directly-in-the-browser-frontend-focus-t1dws71s4) · Frontend Focus · 5 upvotes · 0 comments
- [A Button That Links to a Random Post in HTML & CSS Only – Master.dev Blog](https://daily.dev/posts/a-button-that-links-to-a-random-post-in-html-css-only-master-dev-blog-gzrkkvvhg) · Frontend Masters · 3 upvotes · 1 comments
- [Extreme Server Side Rendering](https://daily.dev/posts/extreme-server-side-rendering-lpstryhfi) · Lobsters · 1 upvotes · 0 comments

---

Tags: [#security](https://daily.dev/tags/security), [#webdev](https://daily.dev/tags/webdev), [#javascript](https://daily.dev/tags/javascript), [#html](https://daily.dev/tags/html)

[View this post on daily.dev](https://daily.dev/posts/how-to-load-a-random-html-page-inline-with-javascript-lg49qvbvs)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"How to Load a Random HTML Page Inline with JavaScript","url":"https://daily.dev/posts/how-to-load-a-random-html-page-inline-with-javascript-lg49qvbvs","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/how-to-load-a-random-html-page-inline-with-javascript-lg49qvbvs"},"datePublished":"2026-11-28T06:30:38.051Z","dateModified":"2026-11-28T06:31:02.369Z","description":"You can show a different HTML page on every visit by picking a random file from an array, loading it with fetch, and setting it as a div's innerHTML. The code...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/b76de9432a1288423e1b9acb011ee4ea?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/b76de9432a1288423e1b9acb011ee4ea?_a=AQAEuop","isAccessibleForFree":true,"articleSection":"Kirupa","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"Kirupa","logo":"https://media.daily.dev/image/upload/t_logo,f_auto/v1/logos/ff2fdb4927d34b81bd07cd18d6049a98","url":"https://daily.dev/sources/kirupa"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/how-to-load-a-random-html-page-inline-with-javascript-lg49qvbvs","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":0},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"security,webdev,javascript,html","timeRequired":"PT18M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Kirupa","item":"https://daily.dev/sources/kirupa"},{"@type":"ListItem","position":3,"name":"How to Load a Random HTML Page Inline with JavaScript"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/how-to-load-a-random-html-page-inline-with-javascript-lg49qvbvs#faq","mainEntity":[{"@type":"Question","name":"Why does fetch fail when I open my HTML file directly from disk?","acceptedAnswer":{"@type":"Answer","text":"Browsers don't let fetch read files through a file:// address, so loading a local HTML file by double-clicking it fails. You need to serve the page through a web server, even a tiny local one. Running python3 -m http.server from the project folder and opening http://localhost:8000 works, as does VS Code's Live Server extension. Developers debugging local fetch quirks keep up with web platform gotchas on daily.dev."}},{"@type":"Question","name":"Is it safe to use innerHTML to insert HTML I fetched into a div?","acceptedAnswer":{"@type":"Answer","text":"It is safe only when you wrote or trust the HTML. Script tags inserted through innerHTML won't run, but event handler attributes like an image's onerror will, which is how cross-site scripting sneaks in. Use textContent for plain text, sanitize untrusted HTML with DOMPurify, or show someone else's page in an iframe. Anyone shipping dynamic HTML insertion can follow XSS and sanitization practices on daily.dev."}},{"@type":"Question","name":"Why do styles from a fetched full HTML page affect my whole page when I use innerHTML?","acceptedAnswer":{"@type":"Answer","text":"Setting innerHTML drops the html, head and body tags but keeps style, title and meta elements, so a loaded style tag now applies to the entire page. Loaded HTML is also styled by the parent page's CSS. Keep loaded pages as small fragments, or parse the text with DOMParser and insert only its body's child nodes. Developers untangling CSS leakage will find more front-end gotchas on daily.dev."}}]}
```

