473,378 Members | 1,140 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Need help with srcElement on unload event

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
12 4275
gits
5,390 Expert Mod 4TB
hi ...

welcome to TSDN ...

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

kind regards
Sep 24 '07 #2
pbmods
5,821 Expert 4TB
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
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">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<script>
function showSource() {
var x= window.event.srcElement;
alert("Source Elemt "+x);
var y = window.event.type;
alert("Source Event "+y);
}
</script>
</HEAD>
<BODY onUnload="showSource();">
<A id="google" HREF="http://www.google.com">Google</A>
<A id="yahoo" HREF="http://www.yahoo.com" >Yahoo</A>
</BODY>
</HTML>[/HTML]
Sep 26 '07 #4
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
gits
5,390 Expert Mod 4TB
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
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
gits
5,390 Expert Mod 4TB
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;
  7.  
  8.     function showSource() {
  9.         if (dont_popup) return;
  10.  
  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="http://www.google.com" onclick="dont_popup = true;">Google</A>
  20.  <A id="yahoo" HREF="http://www.yahoo.com" onclick="dont_popup = true;">Yahoo</A>
  21.  </BODY>
  22. </HTML>
  23.  
kind regards
Sep 26 '07 #8
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
gits
5,390 Expert Mod 4TB
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(event.target.nodeName);
  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
mrhoo
428 256MB
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
So there is no way i can detect the srcElement in a window unload correct ??
Oct 1 '07 #12
gits
5,390 Expert Mod 4TB
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

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

Similar topics

1
by: David A. Beck | last post by:
I have a frameset with an index frame and a main frame. The aspx pages in the main frame are loaded based on the hyperlinks clicked in the index frame. In any aspx page in the main frame I want to...
1
by: hal | last post by:
I have an application that includes an activex component that consumes resources that must be released when the a page is unloaded. Toward this end I subscribe to the unload event of the body...
1
by: Hal | last post by:
My most sincere gratitude to anyone who can help me work around this! I have work that needs to be done in javascript on the client whenever a page is unloaded. To this end, I subscribe to...
6
by: Mike | last post by:
I have a web form in my application that will be used for both viewing and updating information. I have a requirement that if any data has been changed on the page and the user attempts to leave...
3
by: Gauthier Segay | last post by:
Hello, I've an application where all my pages implement a PAGE_CODE string property, this property is stored in HttpContext.Current.Items. In some page, I must persist data in session while...
3
by: ABC | last post by:
Has there Page unload event available? I want to write some codes to release memory. But I can not found the Page_Unload event.
3
by: lesperancer | last post by:
I've got application A, with a hidden form, whose unload event is cancelled unless you use an 'exit' button (so I can do special processing) this works fine when using just application A and it...
1
by: kpvenkatesan | last post by:
Hi all, Is there any possibilities of having unload event in web service like wise in windows applications. I need to terminate a db session object on that unload event so that, the db session need...
11
by: Stevo | last post by:
I've been using the unload event for a long time. I have this code, which I've abstracted and made into a stripped down simple test case below, and it works fine on the major browsers (IE5+,...
1
by: abirami elango | last post by:
Hi, i have created a web application in vb.net. i have assigned a value to the textbox during page UNLOAD event as below.. ......... Protected Sub Page_Unload(ByVal sender As Object, ByVal e As...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.