4

I have many pages on a site im developing, in which each page has its own title tag. In many cases the titles have constant text, for example:

Home | Section 1 | Page 1
Home | Section 1 | Page 2
Home | Section 1 | Page 3, etc...

Inserting these titles makes for harder changes later on, not to mention the hassle of remembering to add them. I'm looking to find a method of dynamically generating the title of each page with javascript, based off the location of the page within the folder structure of the site.

Im currently using a script that does something like this for breadcrumbs, but am not sure how to either reference the same script, or build a similar one for page titles. The breadcrumb script is here: http://trcreative.us/dev/jmsracing/js/breadcrumbs.js

and for the most part doing exactly what I need for titles, minus links per each breadcrumb item. (I don't want it to do that for title's obviously).

See breadcrumbs applied here: http://trcreative.us/dev/jmsracing/races/pigman-long-and-olympic/

Any help would be appreciated. Thank you

2
  • If you're building a big site your CMS or Framework must already be able to do this. Commented Sep 16, 2012 at 2:10
  • I'm building the site custom. Not through a CMS Like wordpress or Joomla. Developing pages and uploading files to a server basically. Commented Sep 16, 2012 at 5:06

4 Answers 4

7

The document object has a title property that can be explicitly set like this:

document.title = "some_title_here";
Sign up to request clarification or add additional context in comments.

1 Comment

I'm not trying to replace the title. I'm trying to generate a dynamic title bases on the location of the page within the folder structure of my site.
6

Assuming that your url's are as following home/section/page.html you could use this function at the top of any page and it will set the title based on the url if that's what you mean.

function setTitle(extra) {
  document.title = location.pathname.split('/').map(function(v) {
    return v.replace(/\.(html|php)/, '')
      .replace(/^\w/, function(a) { return a.toUpperCase(); });
  }).join(' | ') + (extra && ' | '+ extra);
}

Using this function will generate a title like this: Home | Section | Page. If you want to add specific stuff for a particular page then pass a string as the extra parameter and it will be added to the title ie.

// url: http://mypage.com/home/about/frogs    
setTitle('Frogs are awesome');
console.log(document.title); //=> Home | About | Frogs | Frogs are awesome

9 Comments

This is exactly what I'm looking for. Thank you! However it doesn't seem to be working on my page. I placed the script inside of script tags, but on upload, it doesn't apply to the title of the page as it should. Please advise...
I figured out how to emplament the code for the most part, however I have a couple questions. 1) How do i eliminate certain special characters in the dynamic display titles, such as dashes and underscores. I tried adding a couple lines of code unsuccessfully. 2.) How do I make it so, instead of adding additional text at the end of the string (as shown in your example), my unique page value replaces only dynamic page value. For example, adding "Frogs are awsome" would replace the dynamic value of "Frogs".
Lastly, 3) How can I set the default Root value to something unique like "Home"
1) Use regexp and replace() your characters with '' 2) Remove the last item in the array conditionally with pop() 3) Add one more parameter root and remove first item of array conditionally with shift() then prepend root
I'll give it a shot, but im no JS expert yet :) Could you show me a modified example of your original code. View my most recent attempt here: view-source:trcreative.us/dev/jmsracing/races/pigman-long-and-olympic/…
|
3

You can use something like (if using javascript)

var getTitleOfPage = function(){
    // logic here to work out the title of the page.
};

var title = getTitleOfPage();
document.title = title;

Alternatively you may be able to use what ever server side language you are using to set the title also.

1 Comment

Ideally you may even be able to incorporate this into your breadcrumb script if you find it's duplicating code.
1

Try this code in JQuery

<script type="text/javascript">
  $(document).ready(function() {
    document.title = 'your page title goes here';
  });
</script>

2 Comments

How is this different than adding a title to the page. I would still need to have this at page level. Idealy where you've added 'your page title goes here', i would want the dynamic generated title (like in my breadcrumbs
Can you explain where this should be added to the HTML page to replace the title. All I'm seeing on my test page is the static title I have added. The code is not applying.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.