Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9207853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:28:47+00:00 2026-06-18T00:28:47+00:00

I’ve got a problem with the onunload event testing script it must fire only

  • 0

I’ve got a problem with the onunload event testing script
it must fire only on the window onunload
but it fires with load as well.. Why?
There is something wrong, and I don’t know what it is..
Please help

Here is the html code

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript">
function hello(){alert("welcome to my page");}
</script>
<script type="application/javascript" src="script.js"></script>
</head>
<body onload="hello()" >
</body>
</html>

and here’s the script code

window.onunload = goodbye();
function goodbye(){
alert("Thank you for visiting.come back soon!");
}
//window.addEventListener('unload',goodbye(),false);

I’ve tried the inline calling with body tag, the window.onunload calling and the listener calling.
Nothing works correctly, and I get the “Thank you for visiting. come back soon!” message when the page first loads.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T00:28:49+00:00Added an answer on June 18, 2026 at 12:28 am

    The problem is the following line:

    window.onunload = goodbye();
    

    This sets the window.unload event equal to the result of the goodbye() function, and calls the goodbye() function immediately. Change this to:

    window.onunload = goodbye;
    

    IN MORE DETAIL:

    You call a function by naming it, and passing it zero or more parameters in brackets (eg: goodbye()). When you call a function, it is called immediately, and, if it is used as part of a statement (rather than the whole statement), then ther return value of the function is used where the call was made. So, what you’re saying with the line:

    window.onunload = goodbye();
    

    is: “fetch me the result of calling the goodbye function, and assign the return value to the window.onunload variable”. Calling the goodbye function pops up an alert, but returns null, so you are, in effect, saying, “pop up an alert right now, and then set the window.onunload variable to null”.

    The window.onunload variable (and all other event variables) expects to be a function, so what you actually want to say here is, “set the window.onunload function to be the goodbye function, so when the system calls the onunload event, it will call the goodbye function”, and to do that, you assign the function itself to the variable:

    window.onunload = goodbye;
    

    HOWEVER

    The onunload event is probably not what you’re looking for. This only fires after the page has been unloaded, so most of the UI components are not available to your anymore, including alert. Try looking into onbeforeunload instead (JavaScript + onbeforeunload)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I
I am using the SimpleRSS gem to parse a WordPress RSS feed. The only
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have an autohotkey script which looks up a word in a bilingual dictionary
This could be a duplicate question, but I have no idea what search terms

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.