/    Sign up×
Community /Pin to ProfileBookmark

Button to close the Browser

I am a complete newby and am asking for help.
On my single pure HTML-Page I want a button
to close the window and at the same time close
the browser – without any warning messages.
Could someone supply me with the complete
JavaScript code to accomplish this.
Thank you in Advance for any helping advice.

to post a comment
HTMLJavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@cootheadApr 14.2021 — Hi there Ruedy,

and a warm welcome to these forums.

You could try this...

<i>
</i>&lt;!DOCTYPE HTML&gt;
&lt;html lang="en"&gt;
&lt;head&gt;

&lt;meta charset="utf-8"&gt;
&lt;meta name="viewport" content="width=device-width,height=device-height,initial-scale=1"&gt;

&lt;title&gt;Javascript window.close()&lt;/title&gt;

&lt;!--
The internal CSS should be transferred to an external file
&lt;link rel="stylesheet" href="screen.css" media="screen"&gt;
--&gt;

&lt;style media="screen"&gt;
body {
background-color: #f0f0f0;
font: normal 1em / 1.5em sans-serif;
text-align: center;
}

h1 {
font-size: 1.5em;
color: #555;
}
&lt;/style&gt;

&lt;/head&gt;
&lt;body&gt;

&lt;h1&gt;This button may or may not close the window!&lt;/h1&gt;

&lt;button&gt;close&lt;/button&gt;

&lt;script&gt;
(function( w, d ) {
'use strict';

d.querySelector('button').addEventListener( 'click',
function(){
w.close();
},
false );

}( window, document ));
&lt;/script&gt;

&lt;/body&gt;
&lt;/html&gt;


...but I cannot guarantee that all browsers will run the code.

[i]coothead[/i]
Copy linkTweet thisAlerts:
@NogDogApr 14.2021 — > @coothead#1630333 but I cannot guarantee that all browsers will run the code

I sincerely hope that no browser allows itself to be completely closed by a page running in one of its tabs. (Unexpectedly closing the tab might be annoying, but the entire browser?!)
Copy linkTweet thisAlerts:
@cootheadApr 14.2021 — Hi there NogDog,

I only tested the code with [i]Firefox[/i] and [i]Vivaldi[/i]

and both just closed the [b]tab[/b].

I was surprised that it even did that as It was

my understanding that [b]window.close()[/b] only

worked if it had previously been opened with

[b]window.open()[/b].

[url=https://developer.mozilla.org/en-US/docs/Web/API/Window/close][color=#00f][u]MDN - window.close()[/u][/color][/url]

[i]coothead[/i]
Copy linkTweet thisAlerts:
@VITSUSAApr 15.2021 — I agree with coothead. I tested the code, it is working
×

Success!

Help @Ruedy spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...