How to set the title of the browser in iOS in JS when it comes to web Front-end, browser differences are unavoidable. This project encountered the following problems:
The content in the navigation bar is directly set by the title in the project. However, the current project is a single-page application. The whole page will only be completely refreshed for the first time, and only partial refresh will be followed. Therefore, the title can only be dynamically modified through js during page refresh. First, we use the following methods:
Document. title = "content to be displayed in the navigation bar"; $ ("title "). text ("content to be displayed in the navigation bar"); document. getElementsByTagName ("title") [0]. innerText = "content to be displayed in the navigation bar"
The above method is simple and convenient. Unfortunately, the above method is correct on Android, but the iOS browser is invalid.
Solution:
var $body = $('body');document.title = 'the title you want to set';var $iframe = $("");$iframe.on('load',function() {setTimeout(function() {$iframe.off('load').remove();}, 0);}).appendTo($body);
The principle is relatively simple, because the browser does not listen to the change event of document. title after loading the page for the first time and initializing the title. After the title is modified here, add an iframe with blank content to the page, and then immediately delete the iframe. At this time, the title will be refreshed. However, when the iframe is loaded and deleted, the iOS page will flash for several milliseconds (with a gray box), and the Android page will not disappear if there is a gray box, when loading iframe, set the iframe Style to: display: none; to solve this problem, if the iframe is out of the text stream, loading and deleting the iframe will not change the text stream or trigger page rendering.