modifying anchor elements

V

v

Hi guys,
Is it possible to use a simple code to modify all the anchor elements
on a web page to trigger a function

eg to display the link innnertext on the window status upon mouseover?

....Or do I have to laboriously do it for every single link?

thanx,
v
 
D

Dietmar Meier

v said:
Is it possible to use a simple code to modify all the anchor elements
on a web page to trigger a function

eg to display the link innnertext on the window status upon mouseover?

The status bar content is an important information for most users, so
you are not supposed to change it's functionality. If you want to do
so anyway, you can use something like this:

function linkMOut() {
window.status = window.defaultStatus;
}
function linkMOver() {
var s;
if (this && (s = this.text || this.innerText)) {
window.status = s
return true;
}
else return false;
}
function initLinks() {
var i, j, k, oDoc = arguments[0] || window.document;
if (oDoc && (k = oDoc.links) && (j = k.length))
for (i=0; i<j; i++) {
k.onmouseover = linkMOver;
k.onmouseout = linkMOut;
}
if ((k = oDoc.layers) && (j = k.length))
for (i=0; i<j; i++)
if (k.document)
initLinks(k.document);
}
window.onload = initLinks;

ciao, dhgm
 
V

v

thanx :)
hmm.. but maybe you're right there.
Would it be possible to do the same with span, p or some other
elements.?

v

v said:
Is it possible to use a simple code to modify all the anchor elements
on a web page to trigger a function

eg to display the link innnertext on the window status upon mouseover?

The status bar content is an important information for most users, so
you are not supposed to change it's functionality. If you want to do
so anyway, you can use something like this:

function linkMOut() {
window.status = window.defaultStatus;
}
function linkMOver() {
var s;
if (this && (s = this.text || this.innerText)) {
window.status = s
return true;
}
else return false;
}
function initLinks() {
var i, j, k, oDoc = arguments[0] || window.document;
if (oDoc && (k = oDoc.links) && (j = k.length))
for (i=0; i<j; i++) {
k.onmouseover = linkMOver;
k.onmouseout = linkMOut;
}
if ((k = oDoc.layers) && (j = k.length))
for (i=0; i<j; i++)
if (k.document)
initLinks(k.document);
}
window.onload = initLinks;

ciao, dhgm
 
F

Fred Oz

v said:
thanx :)
hmm.. but maybe you're right there.
Would it be possible to do the same with span, p or some other
elements.?

Please don't top post. In answer to the question, yes, but
there are better ways of achieving the same result.

[...]
innerText is IE only and therefore will not work for an
increasing number of your visitors.

Most browsers display information in the status bar that, for
anchors, says where the link is going to. Some will also advise
if the link will open in a new window or tab too.

What is the point of showing the innnerText? Isn't it already
displayed on the screen as part of the document?

<a href="http://www.apple.com"
onmouseover="alert(this.innerText);">Here is a link</a>

Will display an alert with "Here is a link" for IE and
"undefined" for other browsers. A more cross-browser method is
available using DOM methods, but it seems pointless.

If you want a tool-tip, use the title attribute on your links:

<a href="http://www.apple.com"
title="Here is a link to Apple">Apple</a>

No JavaScript required at all. Otherwise, the method
described by Dietmar for attaching events is, in general, fine.
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top