Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > can anyone fix this javascript?

can anyone fix this javascript?
Thread Tools
Junior Member
Join Date: Nov 2004
Location: Michigan
Status: Offline
Reply With Quote
Dec 6, 2004, 06:44 PM
 
http://forums.devshed.com/t35298/s.html

I'm trying to do the same thing this guy is asking for but the solution someone provided doesn't work. Does anyone know how to fix it?
     
Grizzled Veteran
Join Date: Nov 2003
Location: Hebburn, UK
Status: Offline
Reply With Quote
Dec 7, 2004, 04:12 AM
 
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
     
ctlq  (op)
Junior Member
Join Date: Nov 2004
Location: Michigan
Status: Offline
Reply With Quote
Dec 7, 2004, 09:00 AM
 
<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
Reply With Quote
Dec 7, 2004, 10:05 AM
 
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
     
ctlq  (op)
Junior Member
Join Date: Nov 2004
Location: Michigan
Status: Offline
Reply With Quote
Dec 7, 2004, 10:34 AM
 
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
Reply With Quote
Dec 7, 2004, 10:45 AM
 
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
     
ctlq  (op)
Junior Member
Join Date: Nov 2004
Location: Michigan
Status: Offline
Reply With Quote
Dec 7, 2004, 10:54 AM
 
I've tried in firefox 1, ie6 win, safari 1.3 DP5, camino.
Try this link:
http://msu.edu/~youngad6/macnn.html
     
Grizzled Veteran
Join Date: Nov 2003
Location: Hebburn, UK
Status: Offline
Reply With Quote
Dec 7, 2004, 11:20 AM
 
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
     
ctlq  (op)
Junior Member
Join Date: Nov 2004
Location: Michigan
Status: Offline
Reply With Quote
Dec 7, 2004, 11:22 AM
 
It works! Thanks a ton!!!
     
ctlq  (op)
Junior Member
Join Date: Nov 2004
Location: Michigan
Status: Offline
Reply With Quote
Dec 7, 2004, 11:49 AM
 
Actually, one more thing. (I know I'm pushing my luck ) Now the links show up in the statusbar as:
http://somewhere.com/myprog.php?targ.../www.macnn.com

Is there a way to make the link only show up as the target= part in the statusbar when the mouse goes over it?
(Last edited by ctlq; Dec 7, 2004 at 11:55 AM. )
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 09:24 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2