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

Is it possible to set up an event handler or something else so that when *any* link on the page is clicked it 'fires-up' ?

Is it possible to set up an event handler or something else
so that when *any* link on the page is clicked it 'fires-up',
executes some JS and then continues to process the link
that was clicked?
(Without having JS or 'onClick' added to each & every link?)

I've looked everywhere but can't find out how,
or if it's even possible via Javascript...

Regards.
Jul 23 '05 #1
4 1724
Newbie wrote:
Is it possible to set up an event handler or something else
so that when *any* link on the page is clicked it 'fires-up',
executes some JS and then continues to process the link
that was clicked?


You might want to take a look at http://dorward.me.uk/js/global.js

The function 'fragHLlink()' is called onload and adds an event handler to
every a element with an href attribute with a value starting with a '#'.
You should be able to modify it to do what you want.

--
David Dorward <http://blog.dorward.me.uk/> <http://dorward.me.uk/>
Home is where the ~/.bashrc is
Jul 23 '05 #2
On Sat, 07 Aug 2004 06:58:53 GMT, Newbie <ne****@fake.email.net> wrote:
Is it possible to set up an event handler or something else
so that when *any* link on the page is clicked it 'fires-up',
executes some JS and then continues to process the link
that was clicked?
(Without having JS or 'onClick' added to each & every link?)
In theory, yes, but in practice, no. In the event model defined by the
World Wide Web Consortium's (W3C) Document Object Model (DOM) Level 2
Events specification, the first phase that an event goes through is called
capturing. In this phase, the event object that represents the action
moves from the document root (the parent of the HTML element) down to the
target element. For example:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">

<html>
<head>
<title>Example</title>
</head>

<body>
<div>
<a href="somePage.html">A link</a>
</div>
</body>
</html>

When the user clicks on the link, a click event moves from the root, to
HTML, to BODY, to DIV, to A. The DOM 2 Events specification allows you to
attach an event listener in "capturing" mode. Listeners in this mode
receive events on their way towards the target.

All this means that you *could* attach a capturing listener on the
document root to listen for the click event. When it occurs, you'd use
Event.target to check that the destination was a link, then do whatever
processing you wanted (including cancelling the event, if you wish).
Unfortunately, IE doesn't implement DOM 2 Events, and it doesn't provide
an equivalent for the capturing phase.

I did write code that would emulate this, but it's probably too much for
this case (it also isn't totally finished).
I've looked everywhere but can't find out how,
or if it's even possible via Javascript...


There is a more workable solution. It involves programatically iterating
through all links in the page and adding a click listener.

function addListener(e, t, l) {
/* You can remove the if statement below as it's not needed by
* this code. I included it simply to be a "good example".
*/
if(('object' == typeof e) &&
('string' == typeof t) &&
('function' == typeof l)) {
if(e.addEventListener) {
e.addEventListener(t, l, false);
} else {
e['on' + t] = l;
}
}
}

function init() { // Call from "onload"
for(var i = 0, l = document.links, n = l.length; i < n; ++i) {
addListener(l[i], 'click', <function>);
}
}

In the init() function above, replace <function> with the name of the
function you called when the user clicks a link.

Hope that helps and good luck,
Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail
Jul 23 '05 #3
"David Dorward" <do*****@yahoo.com> wrote in message news:cf*******************@news.demon.co.uk...
| Newbie wrote:
|
| > Is it possible to set up an event handler or something else
| > so that when *any* link on the page is clicked it 'fires-up',
| > executes some JS and then continues to process the link
| > that was clicked?
|
| You might want to take a look at http://dorward.me.uk/js/global.js
|
| The function 'fragHLlink()' is called onload and adds an event handler to
| every a element with an href attribute with a value starting with a '#'.
| You should be able to modify it to do what you want.
|
| --
| David Dorward <http://blog.dorward.me.uk/> <http://dorward.me.uk/>
| Home is where the ~/.bashrc is

Thanks :-)

Regards
Jul 23 '05 #4
"Michael Winter" <M.******@blueyonder.co.invalid> wrote in message news:opsccwedb0x13kvk@atlantis...
| On Sat, 07 Aug 2004 06:58:53 GMT, Newbie <ne****@fake.email.net> wrote:
|
| > Is it possible to set up an event handler or something else
| > so that when *any* link on the page is clicked it 'fires-up',
| > executes some JS and then continues to process the link
| > that was clicked?
| > (Without having JS or 'onClick' added to each & every link?)
|
| In theory, yes, but in practice, no. In the event model defined by the
| World Wide Web Consortium's (W3C) Document Object Model (DOM) Level 2
| Events specification, the first phase that an event goes through is called
| capturing. In this phase, the event object that represents the action
| moves from the document root (the parent of the HTML element) down to the
| target element. For example:
|
| <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
| "http://www.w3.org/TR/html4/strict.dtd">
|
| <html>
| <head>
| <title>Example</title>
| </head>
|
| <body>
| <div>
| <a href="somePage.html">A link</a>
| </div>
| </body>
| </html>
|
| When the user clicks on the link, a click event moves from the root, to
| HTML, to BODY, to DIV, to A. The DOM 2 Events specification allows you to
| attach an event listener in "capturing" mode. Listeners in this mode
| receive events on their way towards the target.
|
| All this means that you *could* attach a capturing listener on the
| document root to listen for the click event. When it occurs, you'd use
| Event.target to check that the destination was a link, then do whatever
| processing you wanted (including cancelling the event, if you wish).
| Unfortunately, IE doesn't implement DOM 2 Events, and it doesn't provide
| an equivalent for the capturing phase.
|
| I did write code that would emulate this, but it's probably too much for
| this case (it also isn't totally finished).
|
| > I've looked everywhere but can't find out how,
| > or if it's even possible via Javascript...
|
| There is a more workable solution. It involves programatically iterating
| through all links in the page and adding a click listener.
|
| function addListener(e, t, l) {
| /* You can remove the if statement below as it's not needed by
| * this code. I included it simply to be a "good example".
| */
| if(('object' == typeof e) &&
| ('string' == typeof t) &&
| ('function' == typeof l)) {
| if(e.addEventListener) {
| e.addEventListener(t, l, false);
| } else {
| e['on' + t] = l;
| }
| }
| }
|
| function init() { // Call from "onload"
| for(var i = 0, l = document.links, n = l.length; i < n; ++i) {
| addListener(l[i], 'click', <function>);
| }
| }
|
| In the init() function above, replace <function> with the name of the
| function you called when the user clicks a link.
|
| Hope that helps and good luck,
| Mike
|
| --
| Michael Winter
| Replace ".invalid" with ".uk" to reply by e-mail
Thanks :-)

Regards
Jul 23 '05 #5

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

Similar topics

1
by: Stan | last post by:
If a page has a button with event handler private void btnAdd_Click(object sender, System.EventArgs e) { ....... } this event handler fires every time I refresh the page in the browser with...
3
by: Big D | last post by:
I have a class that inherits from System.Web.Ui.Usercontrol... all is basically is is a button which is created progammatically such as: public abstract class BaseFrontEndEditor :...
4
by: TJ | last post by:
Hi, There is one aspx web page that contains usercontrol. In aspx page and usercontrol , there is each submit button... Here is what I want... I want to process something depending on each...
6
by: Steve Booth | last post by:
I have a web form with a button and a placeholder, the button adds a user control to the placeholder (and removes any existing controls). The user control contains a single button. I have done all...
13
by: Richard W | last post by:
I have a very simple web page (ASP.NET) that I am trying to build. On the web page is a checkbox that enables or disables other controls based upon the checked status. However, .NET fails to...
0
by: Demetri | last post by:
I have created a web control that can be rendered as either a linkbutton or a button. It is a ConfirmButton control that allows a developer to force a user to confirm if they intended to click it...
0
by: arlie_maija | last post by:
Hey - I'm writing a control that contains a DataGrid, and I'm unable to get the update event to fire. When I click the update link, the edit event fires. heres the details... my control...
6
by: Murray Hopkins | last post by:
Hi. THE QUESTION: How do I get a reference to my Object when processing an event handler bound to an html element ? CONTEXT: Sorry if it is a bit long. I am developing a JS calendar tool....
3
by: Chamnap | last post by:
Hello everybody, I have one problem. I want to do something after the user finished scrolling. The scroll event fires whenever the user is scrolling. I don't want this actually. Does anyone has...
1
by: cradius | last post by:
I've got a custom user control, with a DataList, that is dynamically added to the page. In the DataList is an ImageButton that, when clicked, fires off the ItemCommand event of the DataList and...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
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...

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.