views:

100

answers:

2

Ok, so here's the skinny...

I've worked out a jQuery function that will first look at the page and search for <cite> tags. Then, it looks at the text contained within it and searches for a hyphen. If it finds one, then everything BEFORE the hyphen is used as the text within the tag. Whereas, everything AFTER the hyphen is used in an onlick event that opens a new window to that url.

Here's what it looks like:

 // Custom function for <cite> tags making them clickable
 $('cite:contains("-")').each(function(){
  var split=$(this).html().match( /([\s\w]+)[\-](.+)$/i );
  $(this).text(split[1]);
  $(this).click(function(){
   window.open( split[2] );
   return false;
  });
 });

And here is how it's used:

<blockquote>
This is quoted text from some article somewhere on the web... 
<cite>Source of Quote - http://quotedsitesource.com&lt;/cite&gt;
</blockquote>

Now, I've got it working perfectly on a static page... See here: http://blatantwasteofspace.com/crapadoodledoo/cite-test.html

However, when I try to implement it as a script that's loaded up in a WordPress theme, it fails miserably! See here: http://blatantwasteofspace.com/at-random/quotes-time I don't understand it... I mean, I'm loading the exact same version of jQuery. At first I thought it might be because I was using wp_enqueue_script('jquery') to load jQuery since it loads up the noconflict version... So, I removed that and just loaded the same version I'm loading in the static page, but still no dice.

Any ideas?

A: 

It could be that your script is loaded before the contents. If you try something like this instead:

$(document).ready(function(){
 $('cite:contains("-")').live('click', function(){
  var split=$(this).html().match( /([\s\w]+)[\-](.+)$/i );
  window.open( split[2] );
  return false;
 });
});

However, that will still show the link in the text and not removing it (and it will do the parsing once you click the tag, not before), but at least it should work.

Jimmy Stenke
It is loaded in a ready function, sorry I guess I should've stated that too... Would it work better if I were to load it in the footer instead of the header? That way the contents of the page could be loaded before the script went into action?
Josh
Yeah, I saw that. Didn't see the example before I wrote it
Jimmy Stenke
+3  A: 

WordPress is automatically turning your ASCII - dashes into en-dashes (seen in the page as &#8211;). This character won't match the ASCII dash in the regex.

(Say no to misguided automatic “smart” typography, kids! En-dash isn't even the right mark as it normally denotes numerical ranges like 1–10. The em-dash ‘—’ would be more suitable here.)

Is there any good reason why the cites shouldn't be actual links? It would also make the processing easier. eg.

<cite><a href="http://blah"&gt;Blah&lt;/a&gt;&lt;/cite&gt;

$('cite a').click(function(e) {
    var pop= window.open(this.href);
    return pop && !pop.closed;
});
bobince
Tested this on the example site with firebug, and bobince is correct. It is a different dash (–) not -, so change that one and it should work. didn't work however, you need to enter it converted.
Jimmy Stenke
w00t! I converted the JS file to UTF-8 to match WP and also changed the en-dash to the em-dash and voila!Thanks a ton man!
Josh
The cites aren't actual links because I was trying to make it as easy as possible for someone to make the source of the quote a clickable element without having to type in another tag.I know it sees like overkill, but you'd be surprised at how many people have asked for stuff like this.
Josh
Josh, you should look at short tags then. You could make a shortcode and have them simply type `[cite]Name - http://link.com[/cite]` and have your shorttag code turn it into valid markup `<cite><a>...</a></cite>` that be more accessible than a jquery fix.
Doug Neiner
OH! That's an excellent idea dcneiner... Thanks for that, I suppose it would probably be quite a bit more efficient than waiting till the page loads and doing it with jQuery.
Josh