Solved

OnUnload Question

Posted on 2001-09-17
8
376 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
 

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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
jquery, dropdown 4 36
Using this to set a value in javascript 1 25
HTML page and JavaScript 2 50
Hovering effect 9 29
I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

708 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now