Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reliably change page title in browser history via JavaScript

Is it possible to change title of the page in the browser history via JavaScript after the page is loaded? Should be cross-browser (of course) and work on browsers that don't support the HTML5 History API, my primary target browser is Chrome.

I've tried a number of approaches, but none of them seem to work reliably. Here is what I've tried last (history.js):

<html>
    <head>
        <title>Standard title</title>
        <script src="https://rawgit.com/browserstate/history.js/master/scripts/bundled-uncompressed/html4%2Bhtml5/native.history.js"></script>
    </head>
    <body>
        <script>
            window.setTimeout(function(){
                document.title = "My custom title";
                History.replaceState({}, "My custom title", window.location.href);
            }, 3000);
        </script>
    </body>
</html>

If I load the history page in Chrom within 3 seconds after page load, I see Standard title, after 3 seconds I get My custom title.

Why I need this: I have a JavaScript-only app (Angular 1) which runs on different environments (dev, test, production). I'd like to display a title like MyApp (<environmentName>), but I don't want to build separate versions of my app per environment. Instead, the app could request the environment info from the backend via AJAX and update the title of the page. All of this works fine (the page title gets updated), but browser history still shows "standard" title.

Is there a way to change title of the page in the browser history as well?

like image 772
lexicore Avatar asked Nov 30 '16 12:11

lexicore


People also ask

How do you change the title of a Web page using JavaScript?

Use the querySelector() Method to Change the Page Title in JavaScript. We can use the document. querySelector() method to pick elements in a document. The title element can be chosen by giving the title element a selector parameter and retrieving the page's main title element.

How do I change a dynamic web page title?

The textContent property of an element returns the text content of a specific node. The title of the page can be changed by assigning the required new title as a string to the textContent property. This will change the title of the website to the preferred title.

How do you change the title of a Web page in HTML?

Changing the Title Tag in your Website's Control Panel You would edit your title tags through the control panel you use for creating and editing website pages. Look for the section in which you can change meta tags; for more information, refer to your CMS provider's support.

Can JavaScript access browser history?

Browser history is not accessible from JavaScript. If you're building your own browser, you may expose the history in whatever way you find meaningful, and then it'll be up to you what custom javascript code you support in order to extract such information.

How to change the page title with JavaScript?

To change the page title with JavaScript, we can set the document.title property. to set the page title to ‘New Title’. To change the page title with JavaScript, we can set the document.title property.

How do I change the title of the page using textcontent?

The textContent property of an element returns the text content of a specific node. The title of the page can be changed by assigning the required new title as a string to the textContent property.

How to access the previous url in the history using JavaScript?

The window.history object can be written without the window prefix. To protect the privacy of the users, there are limitations to how JavaScript can access this object. The history.back () method loads the previous URL in the history list.

How to select the current title element of the page?

Method 2: Using querySelector () Method: The document.querySelector () method is used to select elements in the document. The title element can be selected by specifying the title element in the selector as a parameter. This will return the current title element of the page.


1 Answers

Short answer: there seems no way you can keep different title records for the same url in Chrome.

Disclaimer: I just stumbled upon this question and had no prior experience about it. Nonetheless, the question was so clear and interesting that I couldn't help but do some experiment:

First of all, I agree that the history records (i.e., the page titles) shown in the history tab are NOT so reliable (maybe a bug or cache).

So I decide that I will look into the database file for Chrome history, for example, using DB Browser for SQLite.

To my surprise, Chrome history keeps only one version (the latest) of title for each url. And when I do History.replaceState({}, "My custom title", window.location.href);, the title does get updated in the database.

However, @Bekim's method wouldn't change the title in the database.

like image 178
Terry Li Avatar answered Oct 23 '22 04:10

Terry Li