How does JS set the title of WeChat browser in iOS?

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.