Solved

OnUnload Question

Posted on 2001-09-17
8
382 Views
Last Modified: 2012-06-21
We have a function that should be called in onunload in IE.  But it also gets called when you click an href.  How can I prevent this?
0
Comment
Question by:zacheuschan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 1

Expert Comment

by:rootnash
ID: 6489330
when u click a certain link, that makes your page go to another page reason to call the unload event of your page.

so when do you only want to call your onunload function? can we see the code?
0
 
LVL 5

Expert Comment

by:nilapenn
ID: 6489448
Try this
<SCRIPT LANGUAGE="JavaScript">
<!--
function RorC() {

var top=self.screenTop;
if (top>9000)
{alert('window was closed')}
else
{alert('window was Refreshed')}
}

//-->
</SCRIPT>

</HEAD>
<BODY BGCOLOR="#FFFFFF" onunload="javascript:if (document.all){RorC()} else {var top=window.outerWidth;if
(top==0) {opener.alert('window was closed')} else {opener.alert('window was Refreshed')}}">
</BODY>
0
 
LVL 22

Accepted Solution

by:
CJ_S earned 100 total points
ID: 6489665
<html>
 <head>
  <script language="javascript">
   function document_onload()
   {
      for(i=0;i<document.links.length;i++) document.links[i].onclick=new function(){navigator.linkClicked=true;}
   }
  </script>
 </head>

 <body onload="document_onload()" onunload="if(!navigator.linkClicked) yourfunction()">

 ...all links etc.

 </body>
</html>

0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

Author Comment

by:zacheuschan
ID: 6490576
Here is my code...

<html>
    <head>
        <script>
            function unloadfunc() {
                alert("unloading...");
            }
        </script>
        <LINK REL='stylesheet' HREF='/system/css/menuIE.css' TYPE='text/css'>
    </head>
    <body ONUNLOAD="javascript:unloadfunc(); return false">
        Hello this is my test.
        <p>
        <a href="javascript:alert('trying2...')">second</a>
    </body>
</html>

it looks like a very simple code, but it does not function as expected.  What happens is when I click href which does not do anything or go anywhere except alert the text 'trying2' it execute onunload and prevents onunload from ever being called again in the future when you are really unloading the page.

nilapen:

your code has the same problem when i put the same link like my code, because after calling onunload and alerting the text 'window was Refreshed' although it was never really refreshed, it also prevents onunload from being called again when you are actually closing the window.

CJ_S:

i do not understand how your solution works, can you explain it some more?
0
 
LVL 22

Expert Comment

by:CJ_S
ID: 6491484
It sets the onclick event of all your links. That onlick function then sets a variable to true, which is being evaluated in the onunload, which will only fire if that variable has been set.

That way you do not have to write the same code over and over again in the onclick event of each and every link. Saves time...

Regards,
CJ
0
 
LVL 12

Expert Comment

by:ahosang
ID: 7968734
This question has been abandoned. I will make a recommendation to the moderators on its resolution in a week or so. I appreciate any comments that would help me to make a recommendation.
 
In the absence of responses, I may recommend DELETE unless it is clear to me that it has value as a PAQ. Silence = you don't care
 
ahosang
0
 
LVL 12

Expert Comment

by:ahosang
ID: 8029786
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

points to CJ_S
Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
ahosang
EE Cleanup Volunteer
0
 
LVL 5

Expert Comment

by:Netminder
ID: 8092830
Per recommendation, force-accepted.

Netminder
EE Admin
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question