Hi M.,

jQuery doesn't help much with comments, but you can do this fairly easily with some poj (plain ol' javascript). Keep in mind, though, that using JavaScript to manipulate the HTML source will have no discernible effect on the standard "View Source" content. The only way someone will see the different date is if they use a "View Generated Source" or another such feature of a browser extension.

It's really best to do this sort of thing in your server side code. For example, in PHP:

<!-- this is my comment {<?php print date('Y'); ?>} -->


That said, if there is some other reason for needing to do this with JS, you could do it like this:

$(document).ready(function() {

var hc = $('head')[0].childNodes;
for (var i = 0; i < hc.length; i++) {
  if (hc[i].nodeType == 8) { // comments have a nodeType of 8
    var year = new Date().getFullYear().toString();
    hc[i].nodeValue = hc[i].nodeValue.replace(/2008/,year);
  }
}

});


Hope that helps.


--Karl
____________
Karl Swedberg
www.englishrules.com
www.learningjquery.com




On Jun 25, 2008, at 5:59 AM, M. wrote:


Hello,

I have a HTML page and I've included a HTML comment above my <title>
tag which says:

<!-- this is my comment {2008} -->
<title>the title of my page</title>

everytime the page is loaded I want to use jQuery to insert the
current year in place of 2008, so if some one views the page next year
then the jQuery script will update the comment to read:

<!-- this is my comment {2009} -->
<title>the title of my page</title>

How do I do this in jQuery, I'm new to the library and can't seem to
find an answer to this anywhere on Google.

Many thanks for you help.

Kind regards,
M.

Reply via email to