473,405 Members | 2,404 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,405 software developers and data experts.

onunload action

Hi,
I have an onunload event in the body tag. When it is activated I want to
know if a <a></a> href was clicked and which one. Is there some document
property or any other object available for such a thing?

No, thanks, I don't want to use the onclick or any other event on the <a>
tag.

Thanks

Frank
Jul 23 '05 #1
5 3913
Ivo
"Frank" wrote
I have an onunload event in the body tag. When it is activated I want to
know if a <a></a> href was clicked and which one. Is there some document
property or any other object available for such a thing?

No, thanks, I don't want to use the onclick or any other event on the <a>
tag.


document.body.onunload=function(){
if( document.activeElement &&
document.activeElement.tagName &&
document.activeElement.tagName.toLowerCase()=='a' )
alert(document.activeElement.href);
}

This will not work in every browser, and it will work incorrectly if user
focuses an <a> then changes his mind and types a new url in the address bar,
since the link will be the active element, but not the reason for the
unload.

Perhaps a better approach is to put an onclick handler on every <a> after
all.
This isn't too difficult however, with a loop through all links onload:
for(var
i=0;i<document.links.length;i++)document.links[i].onclick=somefunction;
HTH
Ivo
Jul 23 '05 #2
Frank wrote:
Hi,
I have an onunload event in the body tag. When it is activated I want
to know if a <a></a> href was clicked and which one. Is there some
document property or any other object available for such a thing?

No, thanks, I don't want to use the onclick or any other event on the
<a> tag.


Play with this:

var clicked; // this is a global variable

function init() { // call this onload
var links = document.getElementsByTagName('a');
links[0].prototype.onclick = function() { clicked = this.href };
}

Something like that; the idea is that each click on a link will write it's
href into a global variable -- on unload just check the global's value.

--
If the Internet is a Marx Brothers movie, and Web, e-mail, and IRC are
Groucho, Chico, and Harpo, then Usenet is Zeppo.
Jul 23 '05 #3
Thanks both, you sent me in the right direction. For the moment I decided
on
<SCRIPT FOR="hrefid" EVENT="onclick">
alert('yep');
</script>
This way the <a> does not contain javascript and it serves my purpose.
Frank
"Berislav Lopac" <be************@dimedia.hr> wrote in message
news:cd**********@ls219.htnet.hr...
Frank wrote:
Hi,
I have an onunload event in the body tag. When it is activated I want
to know if a <a></a> href was clicked and which one. Is there some
document property or any other object available for such a thing?

No, thanks, I don't want to use the onclick or any other event on the
<a> tag.


Play with this:

var clicked; // this is a global variable

function init() { // call this onload
var links = document.getElementsByTagName('a');
links[0].prototype.onclick = function() { clicked = this.href };
}

Something like that; the idea is that each click on a link will write it's
href into a global variable -- on unload just check the global's value.

--
If the Internet is a Marx Brothers movie, and Web, e-mail, and IRC are
Groucho, Chico, and Harpo, then Usenet is Zeppo.

Jul 23 '05 #4
Frank wrote:
Thanks both, you sent me in the right direction. For the moment I decided
on
<SCRIPT FOR="hrefid" EVENT="onclick">
alert('yep');
</script>
This way the <a> does not contain javascript and it serves my purpose.
<head>
...
<meta http-equiv="Content-Script-Type" content="text/javascript">
...
</head>

<body>
...
<a href="whatever.htm" onclick="alert('yep'); return false" ...>...</a>
...
</body>

serves it even better (and without an ID) than your proprietary IE nonsense.
If you would have read the FAQ prior to posting, you would have known:

<http://jibbering.com/faq/#FAQ4_24>
[Top post]


<http://jibbering.com/faq/#FAQ2_3>
PointedEars
Jul 23 '05 #5
Frank wrote:
Thanks both, you sent me in the right direction. For the moment I decided
on
<SCRIPT FOR="hrefid" EVENT="onclick">
alert('yep');
</script>
This way the <a> does not contain javascript and it serves my purpose.
<head>
...
<meta http-equiv="Content-Script-Type" content="text/javascript">
...
</head>

<body>
...
<a href="whatever.htm" onclick="alert('yep');" ...>...</a>
...
</body>

serves it even better (and without an ID) than your proprietary IE nonsense.
If you would have read the FAQ prior to posting, you would have known:
<http://jibbering.com/faq/#FAQ4_24>
[Top post]


<http://jibbering.com/faq/#FAQ2_3>
PointedEars
Jul 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Ric Castagna | last post by:
I have a form that I'm trying to handle if the viewer "leaves" without completing the entire thing. The client wants to be advised that someone one "tried" to fill out the application. Most...
6
by: Mel | last post by:
i would like to ignore onUnload action when the user reloads the page !! what can i do ? pleeeez help muai
3
by: Nithya Venkatachalam | last post by:
Hi, Is there anyway to display a confirm msg onUnload of a page? I mean to say if iam calling onUnload=javascript:check(); and in check(){ if(confirm("check?")) {
10
by: bbcrock | last post by:
does anyone have sample code to display a pop up when a user leaves the site? I really don't want to use this technique, but the clients demand it. I'm thinking something like un body onunload,...
5
by: feng | last post by:
I want to use onunload to capture user's action when he clicks on the "x" button to close the browser. Here is how I do this: <SCRIPT FOR="window" EVENT="onunload"> myScript(); </SCRIPT> ...
0
by: feng | last post by:
I want to use onunload to capture user's action when he clicks on the "x" button, the close window button on the upper-right corner of the brower window, to close the browser. Here is how I...
1
by: jvallee | last post by:
I have a shoping cart page wth a registration form. The submit button takes them to GoogleCheckout. I want to call my formmail.cgi with the <body unload=...> tag. However, I get 2 error...
3
by: ccyarm | last post by:
Hello, I need to modify the following code to make it work with Firefox. It works perfectly well with IE. It seems to me that with Firefox all the form values are lost when the onUnload fires up. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.