1

I have a website that has a <title> tag that is something like this: <title>Blog post name &rarr; Site.com</title> and I want the first part of it to be displayed.

So I want only the Blog post name to be displayed. Here is what I did to display the whole title:

Text blah blah blah: <script>document.write(document.title);</script> blah blah blah.

Is there a way I can do this? Thanks!

2 Answers 2

2

Make a split on the document.title to get the first part.

All other things can be left unchanged.

<script>document.write(document.title.split("\u2192")[0]);</script>

\u2192 is the code for the &raar; character

Sign up to request clarification or add additional context in comments.

2 Comments

I think that would change the blog post's title. And I want it to be so that the code could work dynamically on any post that I want to write.
How do get the title of the blog post? is there a variable having the value?
1

Another approach would be to use indexOf and substring:

<script>document.write(document.title.substring(0, document.title.indexOf('&rarr')));</script>

Here is the Fiddle.

Comments

Your Answer

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