How to get the title of an HTML page and return its value in JavaScript

1

I need to know exactly how to return the title of the page:

<title> nome </title>

Returning this way:

  

You are on the 'name' page.

I've seen a question about a few years but I could not.

    
asked by anonymous 09.01.2018 / 12:45

3 answers

3

Just use the title property in document . You can also use this property to change the title of the page.

console.log('Você está na página '${document.title}'');    
console.log('Você está na página ' + document.title); // Sem string template

document.title = 'Novo Título'; // Alterando o título
console.log('Agora o título da página é '${document.title}'');
<title>Teste</title>

See more in document.title in MDN .

    
09.01.2018 / 12:48
2

You have to use javascript for this:

Applying the following code, for example:

document.getElementById("seuelemento").value = document.title;

You assign the title value to a field with the "id" id

How can example

 <input type='text' id='seuelemento'>

The exact javascript code for you to get the title name of a page is:

document.title
    
09.01.2018 / 12:49
0

You can use the DOM function: getElementsByTagName and access the internal content of this TAG.

document.getElementsByTagName("title")[0].innerHTML

The difference is that this function returns a vector with all elements of the TAG type specified.

And therefore you will have to manipulate the correct index of that target element.

    
09.01.2018 / 13:11