0

In my website, I have title that is kind of like this:

<title>Current Title &rarr; Sub Title &rarr; My Site</title>

I want to display the Sub Title part of the title in some of my html elements... so I wrote this JavaScript code to print the title, but only the text in between the 1st and 2nd &rarr;

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

But that code outputs " Sub Title " with a space in front and behind it. Do you know how I can somehow delete the 2 spaces using javascript (without changing the title) to output something like this: "Sub Title"?

Thanks!

Naveed Butt
  • 2,861
  • 6
  • 32
  • 55
Shalin Shah
  • 8,145
  • 6
  • 31
  • 44

1 Answers1

3

Try using JQuery trim over the one you have used already

http://api.jquery.com/jQuery.trim/

like this:

<script type="text/javascript">
    var titlePart = document.title.split("\u2192")[1];
    document.write($.trim(titlePart));
</script>

or if you want to stick to javascript, try using this...

<script type="text/javascript">
    var titlePart = document.title.split("\u2192")[1];
    document.write(titlePart.replace(/^\s+|\s+$/g, ''));
</script>
Naveed Butt
  • 2,861
  • 6
  • 32
  • 55