SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    SitePoint Evangelist Azam.net's Avatar
    Join Date
    Apr 2002
    Location
    UK
    Posts
    479
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Smile How can I add the month to TITLE tag please - possible?

    How can I add the month to the title of a page please?

    Want to do something like

    <TITLE>Special Offers this [MONTH]!</TITLE>

    Tried inserting Javascript I usually use in my HTML to show the show to the title tag, but didn't work of course!

    Code:
    <SCRIPT LANGUAGE="JavaScript">
    
    <!-- Begin
    var months=new Array(13);
    months[1]="January";
    months[2]="February";
    months[3]="March";
    months[4]="April";
    months[5]="May";
    months[6]="June";
    months[7]="July";
    months[8]="August";
    months[9]="September";
    months[10]="October";
    months[11]="November";
    months[12]="December";
    var time=new Date();
    var lmonth=months[time.getMonth() + 1];
    var year=time.getYear();
    document.write(" " + lmonth + " ");
    
    // End -->
    </SCRIPT>
    Many thanks
    Azam Marketing, Inc.
    Award-Winning Digital Marketing and Design Results Since 1997
    Click here to read acclaimed online marketing & design blog

  2. #2
    SitePoint Enthusiast
    Join Date
    Jan 2005
    Location
    Nelson, NZ
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think that by the time Javascript runs on the client's browser, the title has already been established, so you would have to do it by a server side script, such as php, java or asp etc.
    Scryptik Javascript editor - with inbuilt syntax error checker

  3. #3
    &#083;itePoint Aficionado JVLB's Avatar
    Join Date
    Jan 2002
    Location
    N 44 56.537' W 123 3.683'
    Posts
    1,127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The title is a read/write property of the document object. To add the month to the string that is already established as the title in the HTML:

    document.title+=" March";

    Although this will alter the value returned for document.title, all browsers may not rewrite the title as it appears on the window chrome. Using server-side scripts you could effect the change before the page is delivered, ensuring the name you wish is the one displayed.

  4. #4
    SitePoint Evangelist Azam.net's Avatar
    Join Date
    Apr 2002
    Location
    UK
    Posts
    479
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thanks guys! I appreciate the feedback from people more knowledgeable than myself....

    Managed to find a script and have incorporated it here

    But had to remove the TITLE tag altogether and replace it with:

    Code:
    <script language="JavaScript1.2">
    <!--hide
    
    var isnMonth = new
    Array("January","February","March","April","May","June","July","August","Septerber","October","November","December");
    var isnDay = new
    Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday");
    today = new Date () ;
    Year=today.getYear();
    if (Year < 1000)
    Year+=1900
    Date=today.getDate();
    function setdate(){
    document.title="UKHotMovies.com Site-of-the-Week - Enjoy Huge Savings this "+isnMonth[today.getMonth()]+"!"
    }
    if (document.all)
    setdate()
    else if (document.getElementById)
    window.onload=setdate
    //--hide-->
    </script>
    Thing I'm worried about is that, having TITLE generated in such a way will seriously affect search engine ranking as Google etc. don't have a static TITLE to pick up. Would that be correct?
    Azam Marketing, Inc.
    Award-Winning Digital Marketing and Design Results Since 1997
    Click here to read acclaimed online marketing & design blog

  5. #5
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, you should have a title not generated by JavaScript for search engines to see.
    We miss you, Dan Schulz.
    Learn CSS. | X/HTML Validator | CSS validator
    Dynamic Site Solutions
    Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.

  6. #6
    &#083;itePoint Aficionado JVLB's Avatar
    Join Date
    Jan 2002
    Location
    N 44 56.537' W 123 3.683'
    Posts
    1,127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Without a title tag, I don't believe your page will validate.

  7. #7
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    This dynamically changes the title in IE6 and FF1.0 without a page refresh:
    Code:
    <html>
    <head>
    <title>Special Offers</title>
    
    <script type="text/javascript" language="javascript">
    <!-- Hide from browsers without javascript
    
    function change_title()
    {
    	var months = 
    		["January","February","March","April","May","June", "July", 
    		"August", "September", "October", "November", "December"];
    
    	var this_month = new Date().getMonth();
    	document.title += " this " + months[this_month];
    };
    
    // End hiding -->
    </script>
    </head>
    <body>
    
    <div id="d" onclick="change_title()">click me</div>
    
    </body>
    </html>
    If you are worried about search engine hits, why not just put every month in the original title:

    <title>Special Offers "January","February","March","April","May","June", "July",
    "August", "September", "October", "November", "December"</title>

    and then use the onload event to change the title:

    document.title = "Special Offers this " + months[this_month];

    You do realize, that no one ever looks at the title, and they couldn't care less what it says if they do see it?
    Last edited by 7stud; Mar 27, 2005 at 03:17.

  8. #8
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    By the way, you are fighting against the natural order of things:
    Code:
    months[1]="January";
    months[2]="February";
    months[3]="March";
    months[4]="April";
    months[5]="May";
    months[6]="June";
    months[7]="July";
    months[8]="August";
    months[9]="September";
    months[10]="October";
    months[11]="November";
    months[12]="December";
    Why go to all the trouble of starting the array at index 1, when you just have to add one to the month later:

    var time=new Date();
    var lmonth=months[time.getMonth() + 1];

    As I did in my previous post, you can start the array at it's natural 0 index, and forgo adding 1 to the month. It makes the array much easier to declare, and you don't have to do the extra step of adding 1 to the month. I think that's why the months are numbered from 0-11 instead of 1-12 in the first place.

  9. #9
    SitePoint Evangelist Azam.net's Avatar
    Join Date
    Apr 2002
    Location
    UK
    Posts
    479
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thanks so much 7stud. You have been very helpful.

    If you are worried about search engine hits, why not just put every month in the original title:

    <title>Special Offers "January","February","March","April","May","June", "July",
    "August", "September", "October", "November", "December"</title>

    and then use the onload event to change the title:

    document.title = "Special Offers this " + months[this_month];
    Tried doing this, but didn't work. Am not too hot with Javascript. Could you please show me exactly how to do it if it's not too much trouble?

    Many thanks.
    Azam Marketing, Inc.
    Award-Winning Digital Marketing and Design Results Since 1997
    Click here to read acclaimed online marketing & design blog

  10. #10
    &#083;itePoint Aficionado JVLB's Avatar
    Join Date
    Jan 2002
    Location
    N 44 56.537' W 123 3.683'
    Posts
    1,127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    An additional point to consider in favor of server-side scripts, with JavaScript you are depending on the client's clock being correctly set; if it isn't, you may be serving July data on a January day.

  11. #11
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Are you using any server side programming. If so you can use that.

    Gav

    asp: <%= Date() %>
    php <% echo Date() $> ( i think )

  12. #12
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Some people, like me, actually look at a page's title.

    In tabbed browsers it appears in the tabs and it is the default name for bookmarks/favorites.

    This will print out the months number (1 through 12) in PHP.
    Code:
    <?php echo date('n'); ?>
    http://www.php.net/manual/en/function.date.php
    Last edited by Kravvitz; Mar 27, 2005 at 12:15. Reason: typo
    We miss you, Dan Schulz.
    Learn CSS. | X/HTML Validator | CSS validator
    Dynamic Site Solutions
    Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.

  13. #13
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Tried doing this, but didn't work. Am not too hot with Javascript. Could you please show me exactly how to do it if it's not too much trouble?
    ...then hire a web programmer.

  14. #14
    SitePoint Evangelist Azam.net's Avatar
    Join Date
    Apr 2002
    Location
    UK
    Posts
    479
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thanks so much for your help gRoberts and Kravvitz. That's great! Really appreciate your help.
    Azam Marketing, Inc.
    Award-Winning Digital Marketing and Design Results Since 1997
    Click here to read acclaimed online marketing & design blog


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •