Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Mar 2012
    Posts
    11
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Question Make <title> = <h1> ?

    Hi.

    I run a basic encyclopaedic travel website where each webpage has a standard <h1> tag. Page 1 has <h1>Country A</h1>, page 2 has <h1>Country B</h1>, page 3 has <h1>Country C</h1> and so on.

    The <title> tag for each page is based on each <h1> tag. Page 1 has <title>Country A - mywebsite.com</title>, page 2 has <title>Country B - mywebsite.com</title>, and so on.

    If I ever wanted to update the latter part of each <title> tag (e.g. change mywebsite.com to mywebsite.co.uk), I would have to go through the tedious process of updating the title on every single page.

    I am very much a beginner to JavaScript. I am wondering if it is possible to use JavaScript to generate the 'Country X' part of each <title> tag automatically based on each <h1> tag. If it is possible, would it be easy or difficult to implement?

    If what I am asking for is possible, it would be a wonderful solution, as I could move the <title> tag to a PHP include and only need to update it once (rather than hundreds of times) when I want it changed.

    Thanks in advance.

  • #2
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,885
    Thanks
    56
    Thanked 539 Times in 536 Posts
    If I understand correctly, doing
    Code:
    document.title=window.location.hostname;
    will make the document title the hostname (whether it is mywebsite.com or mywebsite.co.uk or whatever). You can add a string onto it as you like.

  • #3
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,033
    Thanks
    0
    Thanked 248 Times in 245 Posts
    Why not do a "Find and Replace in Files" via any text editor that has this feature?

  • #4
    Senior Coder Arbitrator's Avatar
    Join Date
    Mar 2006
    Location
    Splendora, Texas, United States of America
    Posts
    3,300
    Thanks
    28
    Thanked 275 Times in 269 Posts
    Quote Originally Posted by 553311 View Post
    I am very much a beginner to JavaScript. I am wondering if it is possible to use JavaScript to generate the 'Country X' part of each <title> tag automatically based on each <h1> tag. If it is possible, would it be easy or difficult to implement?
    I wouldn't do this using JavaScript.

    An alternative to glenngv's method is to use XSLT. Put <title><xslt:value-of select="//h1"/> - mywebsite.com</title> in a style sheet applying to all of these "Country X" pages and changing that single URL will change it for all of them.
    For every complex problem, there is an answer that is clear, simple, and wrong.


  •  

    Posting Permissions

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