Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#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

#2 2008-10-02 17:16:37

ruud
Developer Emeritus
From: a galaxy far far away
Registered: 2006-06-04
Posts: 5,068
Website

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

#4 2008-10-02 17:45:13

wet
Developer Emeritus
From: Schoerfling, Austria
Registered: 2005-06-06
Posts: 3,330
Website Mastodon

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

#6 2008-10-02 17:53:13

wet
Developer Emeritus
From: Schoerfling, Austria
Registered: 2005-06-06
Posts: 3,330
Website Mastodon

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

#8 2008-10-02 17:58:04

wet
Developer Emeritus
From: Schoerfling, Austria
Registered: 2005-06-06
Posts: 3,330
Website Mastodon

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

#11 2008-10-02 18:29:28

wet
Developer Emeritus
From: Schoerfling, Austria
Registered: 2005-06-06
Posts: 3,330
Website Mastodon

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

Board footer

Powered by FluxBB