Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
#1 2008-10-02 16:54:46
- lee
- Member
- From: Normandy, France
- Registered: 2004-06-17
- Posts: 831
[textile] Javascript in links?
Anyway to write this using textile?
<a href=“http://www.example.com” onClick=“javascript: pageTracker._trackPageview(‘/outgoing/example.com’);”>
Thanks
Lee
Offline
Re: [textile] Javascript in links?
Writing it as plain HTML is probably much much easier to understand than attempting to make textile do this.
Offline
#3 2008-10-02 17:18:44
- lee
- Member
- From: Normandy, France
- Registered: 2004-06-17
- Posts: 831
Re: [textile] Javascript in links?
Thanks, thought as much but had to ask.
Cheers
Lee
Offline
Re: [textile] Javascript in links?
If you have lots of such links, a jQuery solution to patch links by class might come handy.
- Designate links with a class:
"(external)foo":http://example.com/
- In your page’s <HEAD>
, attach an onclick handler at all of these links upon page load:
$(document).ready( function() {
$('a.external').click(function(){pageTracker._trackPageview(this.href);})
}
(requires jQuery)
Last edited by wet (2008-10-02 17:52:38)
Offline
#5 2008-10-02 17:50:54
- lee
- Member
- From: Normandy, France
- Registered: 2004-06-17
- Posts: 831
Re: [textile] Javascript in links?
Wow thanks. Thing is the example.com bit in pageTracker._trackPageview(‘/outgoing/example.com’) has to be unique for each link.
Offline
Re: [textile] Javascript in links?
Yeah, I just noticed this requirement. Sample code amended accordingly.
Offline
#7 2008-10-02 17:54:26
- lee
- Member
- From: Normandy, France
- Registered: 2004-06-17
- Posts: 831
Re: [textile] Javascript in links?
I’m going to try it right now. Thanks again.
Offline
Re: [textile] Javascript in links?
For a test, use this:
$(document).ready( function() {
$('a.external').click(function(){alert(this.href);return false;})
}
Saves a few clicks and reloads…
Offline
#9 2008-10-02 18:17:21
- lee
- Member
- From: Normandy, France
- Registered: 2004-06-17
- Posts: 831
Re: [textile] Javascript in links?
All done, just have to wait for google analytics to update in a few hours. I’ll let you know, but I’m sure it’s going to work.
Best wishes
Lee
Offline
#10 2008-10-02 18:20:13
- lee
- Member
- From: Normandy, France
- Registered: 2004-06-17
- Posts: 831
Re: [textile] Javascript in links?
Not sure if this is going to far or possible, but could external links automatically be given the external class using jquery?
Offline
Re: [textile] Javascript in links?
Depends on whether you can make sure that no internal link is absolute, i.e. does not contain the full URI. This snippet adds an external
class to all links starting with http://
$("a[href^='http://']").addClass('external');
One can probably think of more elaborate versions of this attribute probing.
Last edited by wet (2008-10-02 18:37:25)
Offline
#12 2008-10-02 18:46:09
- lee
- Member
- From: Normandy, France
- Registered: 2004-06-17
- Posts: 831
Re: [textile] Javascript in links?
Didn’t seem to work, the class wasn’t applied to external links.
I have this in the head
<script type=“text/javascript” src=”../textpattern/jquery.js”></script>
<script type=“text/javascript”>
$(document).ready( function() {
$(“a[href^=http://]”).addClass(‘external’);
$(‘a.external’).click(function(){alert(this.href);return false;});
}
</script>
If you haven’t for time for this no worries, you’ve already helped me out no end.
Offline