Search code examples
javascriptuserscriptspage-title

How to remove portions of Web page title using Javascript


I'm new at this Javascript stuff, so please excuse my basic question. I'm trying to find a way to remove some portion of a web page title using Javascript in a userscript. Take for example the title of this page:

How to remove portions of Web page title using Javascript - Stack Overflow

I want to yank out the '-' and Website name so that it becomes:

How to remove portions of Web page title using Javascript

I know how to change the entire title using document.title = "new title";, but that's about the closest I can get. Can someone please help?

Thanks in advance.


Solution

  • This should do it:

    document.title.substr(0,document.title.lastIndexOf("-"));
    

    It removes everything from the last occurrence of "-".

    http://jsfiddle.net/niklasvh/TA69F/