Open a Self-Contained Webpage from a Button Click
If you want a button click to open a new webpage - not an external URL or file - here is how to do it. The code below creates the new page on the fly with JavaScript, using no external file or URL.
Live Preview
Code
On desktop, it opens in a new window with a minimum one-inch gap on all sides of the screen. On mobile, it opens in a new tab.
<!DOCTYPE html>
<html>
<head>
<title>New Webpage from Button</title>
</head>
<body>
<button type="button" id="openBtn">Click Here</button>
<script>
document.getElementById('openBtn').addEventListener('click', function () {
const html = `
<!DOCTYPE html>
<html>
<head>
<title>Second Webpage</title>
<style>
body {padding: 20px; }
</style>
</head>
<body>
<h1>Welcome</h1>
<p>I am second webpage.</p>
<p>This is another line.</p>
<p>And this is a third line.</p>
</body>
</html>
`;
const url = URL.createObjectURL(new Blob([html], { type: 'text/html' }));
const isMobile =
/Android|iPhone|iPad|iPod|Mobi/i.test(navigator.userAgent) ||
(window.matchMedia('(pointer: coarse)').matches && screen.width < 900);
if (isMobile) {
window.open(url, '_blank');
return;
}
const gap = 96;
const chrome = 90;
const screenLeft = screen.availLeft || 0;
const screenTop = screen.availTop || 0;
const left = screenLeft + gap;
const top = screenTop + gap;
const width = screen.availWidth - gap * 2;
const height = screen.availHeight - gap * 2 - chrome;
window.open(
url,
'_blank',
`popup=yes,width=${width},height=${height},left=${left},top=${top}`
);
});
</script>
</body>
</html>
You should know
-
Mobile vs. desktop: Mobile browsers ignore window size and position options and open a new tab instead. The detection in the code makes this behavior explicit and avoids passing popup features on mobile.
-
The "inch" measurement: Browsers define one inch as 96 CSS pixels, which is not always exactly one physical inch on every monitor. Browsers cannot read the true physical size of a screen, so 96px is the standard approach.
-
Height adjustment: The height value in
window.openapplies to the page area. The popup's title bar and address bar are added on top, so thechromevalue (90px) compensates for them. Browsers differ slightly, so you can adjust this value if the bottom gap looks wrong. -
Browser limits: Some browsers restrict popup position or size - for example, they may not allow a popup to extend off-screen. Users can also configure their browsers to always open new tabs instead of popups. This behavior is controlled by the browser, not by your code.
-
Screen vs. taskbar:
availWidthandavailHeightexclude the taskbar, so the gap is measured from the usable screen area rather than the physical edge of the monitor.
Rules for pasting your own HTML inside the Backticks
-
Do not write
</script>literally inside the template if the second page contains a script. The browser will end your main script early. Write it as<\/script>instead. -
Backticks inside your HTML must be escaped as
\`. -
${inside your HTML - for example, in a script on the second page - must be written as\${, because template literals treat it as a variable placeholder. -
Plain HTML, CSS, and normal quotes (
"and') inside the template need no escaping.

