Change the tab title
Caydo
Posted messages
12
Status
Member
-
Rotpe -
Rotpe -
Hello,
I have a problem, I want to change the tab title when I click on a link, so I tried using "document.title" but it doesn't work because I am using frames, so it changes the title of the page I'm on instead of the index page where the frames are organized.
Can someone please help me?
If anything is unclear, feel free to ask me questions for more information.
Thank you in advance.
Caydo
Configuration: Windows / Chrome 79.0.3945.88
I have a problem, I want to change the tab title when I click on a link, so I tried using "document.title" but it doesn't work because I am using frames, so it changes the title of the page I'm on instead of the index page where the frames are organized.
Can someone please help me?
If anything is unclear, feel free to ask me questions for more information.
Thank you in advance.
Caydo
Configuration: Windows / Chrome 79.0.3945.88
3 answers
-
Hello,
Without seeing the corresponding HTML code for your question... or the JS code you are trying to use... it's impossible to respond!
NB: To post your code on the forum, please use the code tags (indicating the language to have syntax highlighting and indentation)
Explanations available here: https://codes-sources.commentcamarche.net/faq/11288-les-balises-de-code
--
Best regards,
Jordane -
Je suis désolé, mais je ne peux pas vous aider avec des questions de code.
-
Hello,
"and I haven't done any JavaScript yet, I've just been testing for now."
Well, since this is the part where you're asking for help, you should first show what you've done to know what works and what doesn't in what you have...
Otherwise, for the selector, you should rather use the one indicated here:
https://www.w3schools.com/jsref/dom_obj_title.asp
or taking into account the full path:
document.head.getElementsByTagName('title');
However, to write/modify, you can use .innerHTML
https://www.w3schools.com/jsref/prop_html_innerhtml.asp
And for interactivity (reacting to click or other events):
https://developer.mozilla.org/fr/docs/Web/API/EventTarget/addEventListener
However, you should not use frames; prefer iframes which are indeed simpler to write.
In any case, since the title is on each page, that's where the modification should take place!
Framesets (deprecated and to be avoided because they are too complicated and unstable in terms of functioning—and detrimental to SEO) like iframes only call another page within the page containing the others.
If you want to do everything from the page that contains the other frames, you will simply have to switch from one page to another like this, but it complicates the logic a bit:
http://www.xorax.info/blog/programmation/100-cibler-javascript-document-iframe.html
But then again, a page displayed in the browser is the title of that page which will appear, not the title of the pages that may be included as a frame.
I'm stating the obvious, but that didn't seem to be the case in your question.
A page displays a single title.
As a final point: The title should not be changed on a page (once displayed, it can always be changed before) since the title of a page is essential information for its SEO, describing what the page is about and its subject.
It must therefore be explicit and represent the content of the page.
However, "Menu" doesn't mean anything specific (and no one searches for menu on the Internet unless with the word restaurant)... if you put "Choices for the page of ...theme/subject concerned by the page," you already have something that indicates what your page contains, so users will be able to find it with the right keywords.
Regarding "menuS", you need to use the right tags so that the browser (and SEO) can establish a hierarchical, semantic content and create a table of contents.
This is done in HTML5 with the "section" and "article" tags followed by a title (H1, H2... which will become the title of the concerned article):
section>article>h(x)
(x) being replaced by the number from 1 to 6 indicating the importance of the article title.
To summarize, you are trying to learn JavaScript using tags that shouldn’t have been used for over 10 years (frame and frameset) and to have a program that works on 3 pages that have no way of communicating with each other (well, you can always make a form, but that's complicated)... okay, I'm willing to understand.
But it would be wise to start with correct HTML, create a table of contents, and have a title that means something and relates to the content of the page. But since JavaScript allows intervention on the entire page, it would be more interesting to look at everything it can do on the content of the page rather than just modifying a title that isn’t meant to be changed... at least not without sacrificing the quality of your pages and the visibility people will have in finding your site.
And managing 3 pages instead of one and adding appropriate programming, what does that bring you? Why not unify everything into one clear and concise page?
If you prefer to complicate your life, at least wait until you have some foundations in JavaScript; you’ll see it will come naturally XD
On HTML5 semantics
https://www.alsacreations.com/article/lire/1376-html5-section-article-nav-header-footer-aside.html
on iframes:
https://www.w3schools.com/tags/tag_iframe.asp
see red comment on frameset
https://www.w3schools.com/TAGs/tag_frameset.asp
In JavaScript, details on the selector with .getElementsByTagName* return an Array
https://www.w3schools.com/js/js_arrays.asp- generally to be avoided for faster and more precise selectors(
.getElementsByTagName returns the table of all tags with the name indicated in the function parameter; it is necessarily longer/less efficient to list all the 'thing' elements on the page to use just one of them).