onunload action

F

Frank

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
 
I

Ivo

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.onclick=somefunction;
HTH
Ivo
 
B

Berislav Lopac

Frank said:
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.
 
F

Frank

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 said:
Frank said:
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.
 
T

Thomas 'PointedEars' Lahn

Frank said:
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:

[Top post]

<http://jibbering.com/faq/#FAQ2_3>


PointedEars
 
T

Thomas 'PointedEars' Lahn

Frank said:
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:


[Top post]

<http://jibbering.com/faq/#FAQ2_3>


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top