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
Solved

OnUnload Question

Posted on 2001-09-17
8
380 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how can i get the string decoded ? 2 28
How to reload page and grab querystring 2 22
Page auto-refreshes when scrolled on iPhone. 5 54
2 separate CSS animations 2 16
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
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…

856 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