views:

83

answers:

3

I am trying to implement 'time-ago' feature in my website using a JQuery timeago plugin. Unfortunately I have encountered some problems.

<script type="text/javascript" charset="utf-8">
jQuery("abbr.timeago").timeago();
</script>

var content = document.getElementById('div');

var html = "<abbr class=timeago title=2008-07-17T09:24:17Z></abbr>" + '<br>';
content.innerHTML += html

<div id='div'></div>

This jquery plugin basically convert timestamp from the title and print it in a nice format for every abbr class=timeago. The problem is that the code above doesn't work, but if I put it within this div it works fine. I think it is maybe because this jquery plugin is applied before generating html by javascript, but I am not sure. Any ideas?

+1  A: 
var html = "<abbr class=timeago title=2008-07-17T09:24:17Z></abbr>" + '<br><br>';

First off, put your attributes in quotes:

var html = "<abbr class=\"timeago\" title=\"2008-07-17T09:24:17Z\"></abbr>" + '<br><br>';

Also, refer to this page: http://www.w3schools.com/TAGS/tag_abbr.asp

The point of the ABBR tag is to give additional information regarding an inline element for crawlers and the like. I'm hard-pressed to think of a reason you might want to use this for a datetime.

I'd say rather than crawling through a plugin you don't know very well trying to find out why it likes divs rather than abbr's, just use the div. If it has to be an inline element, use <span>.

dclowd9901
Quotes aren't always required (and aren't in this case) in HTML. That shouldn't be the problem, although it is good practice to use them all the time. http://www.w3.org/TR/html4/intro/sgmltut.html#h-3.2.2
Andy E
Thanks, I didnt use quotes just for testing purposes because I thouth it might cause the problem. Anyway, I changed it to div, that's fine but it doesn't solve the problem:/
Vafello
+3  A: 

The problem, I bet, is that your statement that initializes the "timeago" plugin is running before you put the <abbr> element into the page. Therefore, when it runs, nothing happens.

Why would you add to the DOM with that primitive code when you've got jQuery anyway?

$('#div').append($('<abbr/>')
  .addClass('timeago')
  .attr('title', '2008-07-17T09:24:17Z')
).append($('<br/>')).append($('<br/>'))
.find('abbr.timeago').timeago();

(I'm assuming that you really do have a <div> element with the "id" value "div".)

The statement:

$('abbr.timeago').timeago();

means, "find all <abbr> elements with the class "timeago", and initialize the "timeago" plugin on each of them." It does not mean, "any time there's an <abbr> element on the page, and it has the class "timeago", well then make sure that the "timeago" plugin is set up for it."

Pointy
Well the reason I am doing this is that I use it within a loop, I have multiple tomeago divs with several different dates. Your code would surely be easier and more elegant, but it is not what I need.
Vafello
If you run the code at the end of the page (or within a $(document).ready() function), it *will* execute for every matching element on the page (in this case, any `<abbr>` with the class of "timeago". Really, all you need is to make sure you have an ISO8601 timestamp in the title"" attribute. http://timeago.yarp.com/
dclowd9901
Yes, but note that in the original question the element is being *added* to the page dynamically by Javascript.
Pointy
@Vafello well I still suspect that your fundamental problem is that you need to call that "timeago" initialization **after** you add the stuff to the page, not before.
Pointy
But with the current code, is it possible to call jquery plugin after the JS is executed?
Vafello
Why wouldn't it be? jQuery is just Javascript. You can call it any time you want to. It certainly won't work if you just call it beforehand.
Pointy
Yeah, but I am not sure how to do it. I wont work if I simply place the code at the end of this html file. Maybe my questions are silly, but I am new to JS. Thanks.
Vafello
Well where is the code that adds the "timeago" sections? Why not just do the jQuery plugin setup right at the end of that?
Pointy
I tried it, but it doesn't work...
Vafello
Well it would help a lot if you'd post more of your code then.
Pointy
A: 

I figured it out myself. It's better to use jQuery.timeago(timestamp) function than deal with abbr and so on.

Vafello