OnUnload Question

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?
zacheuschanAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
CJ_SConnect With a Mentor Commented:
<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
 
rootnashCommented:
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
 
nilapennCommented:
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
zacheuschanAuthor Commented:
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
 
CJ_SCommented:
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
 
ahosangCommented:
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
 
ahosangCommented:
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
 
NetminderCommented:
Per recommendation, force-accepted.

Netminder
EE Admin
0
All Courses

From novice to tech pro — start learning today.