How can I remove specific values from the page title?
Title : STACKOVERFLOW
REMOVE : STACK
RESULT : OVERFLOW
How can I remove specific values from the page title?
Title : STACKOVERFLOW
REMOVE : STACK
RESULT : OVERFLOW
Knowing that you can access the title of the page via document.title
you can then transform that string and re-define the title with document.title = novaString;
.
This will have SEO implications that you should take into account ... (this is a subject already addressed in other questions).
Example:
var titulo = document.title;
document.title = titulo.replace('STACK', ''); // vai mudar o titulo removendo "STACK"
In the case of JavaScript you can use the substring
method!
With the substring
method we take a piece of the string, we pass the initial position followed by the number of characters that must be extracted.
Ex:
var titulo = "Stackoverflow";
var resultado = titulo.substring(5, 15);
console.log(resultado);
Output:
overflow
To get the title of the page, use document.title
var titulo = document.title;
If you wanted to search for a specific value in a string, regardless of its indexes, you can use suaString.indexOf('STACK')
, if you find something, that method will return the index at the beginning of the string (ex: 4 ), if you do not find your answer will be -1.
Example:
var busca = "STACK";
var suaString = "STACKOVERFLOW";
var indexBusca = suaString.indexOf(busca);
// a variavel indexBusca irá armazenar o index do retorno, se for diferente de -1 é porque a string foi encontrada em 'suaString'