By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,915 Members | 1,360 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,915 IT Pros & Developers. It's quick & easy.

Need help with srcElement on unload event

P: 9
I have a page on which i need to show a pop up to the user if he tries to exit the page . I have already written an onunload function which takes care of displaying the popup . But the popup should not be displayed to the user if the user clicks on certain links on the site . These links also cause the page to be unloaded. I am using the srcElement to identify which link has been clicked . Unfortunately , in my function srcElement object is coming null . I am tired of trying please help. If there are any code samples i would be really helpful.
Sep 24 '07 #1
Share this Question
Share on Google+
12 Replies

Expert Mod 5K+
P: 5,389
hi ...

welcome to TSDN ...

please post some code that you have already and that relates to your problem ...

kind regards
Sep 24 '07 #2

Expert 5K+
P: 5,821
Heya, Nileshs. Welcome to TSDN!

Changed thread title to better describe the problem.

Have the specific links set a global variable that your unload function checks for before showing the popup.
Sep 25 '07 #3

P: 9
Thanks guys
Below is a sample code that I have written. I am just trying to make this work .
I will integrate it into my application later.

[HTML]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<TITLE> New Document </TITLE>
function showSource() {
var x= window.event.srcElement;
alert("Source Elemt "+x);
var y = window.event.type;
alert("Source Event "+y);
<BODY onUnload="showSource();">
<A id="google" HREF="">Google</A>
<A id="yahoo" HREF="" >Yahoo</A>
Sep 26 '07 #4

P: 9
I am getting the source element as null . Sorry if i could not be clear earlier
P.S. How do I change the tittle of the post ???
Sep 26 '07 #5

Expert Mod 5K+
P: 5,389
hmmm ...

i think the srcElement of the unload-event is not the clicked one ... it is fired when the location of the window changes. so you may set a flag onclick of your links and check for it in your unload-function ...

kind regards

PS: in case you want to have the title changed tell me the new one and i change it for you
Sep 26 '07 #6

P: 9
Thx for such a quick reply

Can u just post the code using the same example. I would really appreciate it.

Also This looks like a better title
Need help with srcElement on unload event
Sep 26 '07 #7

Expert Mod 5K+
P: 5,389
hi ...

changed the thread title for you ...

try something like this:

Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
  2. <HTML>
  3.  <HEAD>
  4.    <TITLE> New Document </TITLE>
  5.    <script>
  6.     var dont_popup = false;
  8.     function showSource() {
  9.         if (dont_popup) return;
  11.         var x = window.event.srcElement;
  12.         alert("Source Elemt "+x);
  13.         var y = window.event.type;
  14.         alert("Source Event "+y);
  15.     }
  16.     </script>
  17.  </HEAD>
  18.  <BODY onbeforeunload="showSource();">
  19.  <A id="google" HREF="" onclick="dont_popup = true;">Google</A>
  20.  <A id="yahoo" HREF="" onclick="dont_popup = true;">Yahoo</A>
  21.  </BODY>
  22. </HTML>
kind regards
Sep 26 '07 #8

P: 9
Thx a lot man this solved my problem related to the popup.
Still did not understand why we are getting srcElement as null though .....
Sep 26 '07 #9

Expert Mod 5K+
P: 5,389
hi ...

i don't really know either ... but i also don't know what element it should/could be except window or document ... because the event is a handler that is attached to the window/document.

i tested the same in FF with the following:

Expand|Select|Wrap|Line Numbers
  1. window.onbeforeunload = function(event) {
  2.     alert(;
  3. }
and that gave the document as event-target ... may be IE handles it another way ... i didn't find anything that gave me a hint on the event's srcElement.

kind regards
Sep 26 '07 #10

P: 428
the unload and beforeunload events are not fired by the link element you click, but by the window itself, which is not an html element.
Sep 27 '07 #11

P: 9
So there is no way i can detect the srcElement in a window unload correct ??
Oct 1 '07 #12

Expert Mod 5K+
P: 5,389
hi ...

not exactly ... you know that it is the window or document ... but you cannot detect from where it is triggered ... but of course you may, as an example, handle all click events in your page and may react to them in a way you want ...

kind regards
Oct 2 '07 #13

Post your reply

Sign in to post your reply or Sign up for a free account.