 |
 |
can anyone fix this javascript?
|
 |
|
 |
|
Junior Member
Join Date: Nov 2004
Location: Michigan
Status:
Offline
|
|
|
|
|
|
| |
|
|
|
 |
|
 |
|
Grizzled Veteran
Join Date: Nov 2003
Location: Hebburn, UK
Status:
Offline
|
|
The code provided looks like it should work...
What errors is it throwing and how are you putting it in your page?
|
|
Just who are Britain? What do they? Who is them? And why?
Formerly Black Book
|
| |
|
|
|
 |
|
 |
|
Junior Member
Join Date: Nov 2004
Location: Michigan
Status:
Offline
|
|
<html>
<head>
<script type="text/Javascript">
<!--
function AClick(evt) {
var target;
if (typeof window.event != 'undefined') target = event.srcElement;
else if (typeof evt != 'undefined') target = evt.target.parentNode;
if (target.nodeName.toLowerCase() == 'a') window.location.href = 'myprog.php?target=' + target.href;
}
function loadClick() {
if (typeof document.body.addEventListener != 'undefined') document.body.addEventListener('click', AClick, false);
else if (typeof document.body.attachEvent != 'undefined') document.body.attachEvent('onclick', AClick);
}
// -->
</script>
</head>
<body onLoad="loadClick()">
<a href="http://www.macnn.com">macnn</a>
</body>
</html>
|
|
|
| |
|
|
|
 |
|
 |
|
Grizzled Veteran
Join Date: Nov 2003
Location: Hebburn, UK
Status:
Offline
|
|
Ah right, I see.
Basically, if you put an alert(target.nodeName) before the if statement on the nodeName, you'll see what each browser is reporting.
Firefox is reporting that it's BODY - which means you should forget the .parentNode bit. However, to complicate matters, Safari is reporting everything as a #TEXT node. This may be because I'm using Safari 2, you should check for 1.2.
Anyhoo - rather than adding the event listener to the whole body, you could just add it to the anchors... Something like:
Code:
<html>
<head>
<script type="text/Javascript">
<!--
function AClick(e) {
var target;
if (document.addEventListener) {
target = e.target;
if (target.nodeName.toLowerCase() != "a") target = target.parentNode;
} else {
target = event.srcElement;
}
window.location.href = 'myprog.php?target=' + target.href;
}
function loadClick() {
var a = document.getElementsByTagName("BODY")[0].getElementsByTagName("A");
for (var i=0; i < a.length; i++) {
if (document.addEventListener) {
a[i].addEventListener("click", AClick, true);
} else {
a[i].attachEvent("onclick", AClick);
}
}
}
// -->
</script>
</head>
<body onLoad="loadClick()">
<a href="http://www.macnn.com">macnn</a>
<a href="http://www.macnn.com">macnn</a>
<div><a href="http://www.macnn.com">macnn</a></div>
<div>
<span><a href="http://www.macnn.com">macnn</a></span>
<a href="http://www.macnn.com">macnn</a>
</div>
</body>
</html>
Note that IE 5 Mac doesn't support attachEvent, so it won't work in that. It should work in IE Win though. I added in a bunch of links nested inside other elements to show that it will pick up every link on the oage.
|
|
Just who are Britain? What do they? Who is them? And why?
Formerly Black Book
|
| |
|
|
|
 |
|
 |
|
Junior Member
Join Date: Nov 2004
Location: Michigan
Status:
Offline
|
|
I tried your code and couldn't get it to work in any browsers. How'd you get it to work?
|
|
|
| |
|
|
|
 |
|
 |
|
Grizzled Veteran
Join Date: Nov 2003
Location: Hebburn, UK
Status:
Offline
|
|
Just save what I pasted as a .html document and open it in a browser... You must have a file called myprog.php in the same directory to actually see it working, obviously.
What browsers are you trying it in? I tried it in IE6, Firefox 1 and Safari 2 just now.
|
|
Just who are Britain? What do they? Who is them? And why?
Formerly Black Book
|
| |
|
|
|
 |
|
 |
|
Junior Member
Join Date: Nov 2004
Location: Michigan
Status:
Offline
|
|
|
|
|
|
| |
|
|
|
 |
|
 |
|
Grizzled Veteran
Join Date: Nov 2003
Location: Hebburn, UK
Status:
Offline
|
|
Ah, use this instead, it's much easier:
Code:
function loadClick() {
var a = document.getElementsByTagName("BODY")[0].getElementsByTagName("A");
for (var i=0; i < a.length; i++) {
a[i].setAttribute("href", "myprog.php?target=" + a[i].getAttribute("href"));
}
}
|
|
Just who are Britain? What do they? Who is them? And why?
Formerly Black Book
|
| |
|
|
|
 |
|
 |
|
Junior Member
Join Date: Nov 2004
Location: Michigan
Status:
Offline
|
|
It works! Thanks a ton!!!
|
|
|
| |
|
|
|
 |
|
 |
|
Junior Member
Join Date: Nov 2004
Location: Michigan
Status:
Offline
|
|
|
(Last edited by ctlq; Dec 7, 2004 at 11:55 AM.
)
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|