Solved

OnUnload Question

Posted on 2001-09-17
8
378 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
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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
This article discusses how to create an extensible mechanism for linked drop downs.
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…

808 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