简体   繁体   English

在内容完全加载之前不显示页面

[英]Don't show page until content has fully loaded

I am creating a landing page which should exist in two languages. 我正在创建应该以两种语言存在的登录页面。 The texts that should be shown are in two JSON files, called accordingly "ru.json" and "en.json". 应该显示的文本位于两个JSON文件中,分别称为“ ru.json”和“ en.json”。 When a user clicks on the "Change language" button, the following function is executed: 当用户单击“更改语言”按钮时,将执行以下功能:

function changeLang(){
if (userLang == 'ru') {
    userLang = 'en';
    document.cookie = 'language=en';
}
else {
    userLang = 'ru';
    document.cookie = 'language=ru';
}

var translate = new Translate();
var attributeName = 'data-tag';
translate.init(attributeName, userLang);
translate.process();
}

Where Translate() is the following: 其中Translate()如下:

function Translate() {
//initialization
this.init =  function(attribute, lng){
    this.attribute = attribute;
    if (lng !== 'en' && lng !== 'ru') {
        this.lng = 'en'
    }
    else {
        this.lng = lng;
    }
};
//translate
this.process = function(){
    _self = this;
    var xrhFile = new XMLHttpRequest();
    //load content data
    xrhFile.open("GET", "./resources/js/"+this.lng+".json", false);
    xrhFile.onreadystatechange = function ()
    {
        if(xrhFile.readyState === 4)
        {
            if(xrhFile.status === 200 || xrhFile.status == 0)
            {
                var LngObject = JSON.parse(xrhFile.responseText);
                var allDom = document.getElementsByTagName("*");
                for(var i =0; i < allDom.length; i++){
                    var elem = allDom[i];
                    var key = elem.getAttribute(_self.attribute);

                    if(key != null) {
                        elem.innerHTML = LngObject[key]  ;
                    }
                }

            }
        }
    };
    xrhFile.send();
}

Everything works fine, however, when a user opens the page for the first time, if his Internet connection is bad, he just sees the elements of the page without text. 一切正常,但是,当用户第一次打开页面时,如果他的Internet连接不正常,他只会看到页面中没有文本的元素。 It is just 1-2 seconds, but still annoying. 虽然只有1-2秒,但仍然很烦人。

The question is, is there any way to check the text has loaded and display the page elements only on this condition? 问题是,是否有任何方法可以检查文本是否已加载并仅在这种情况下显示页面元素?

You can use $(document).ready() in this way 您可以通过这种方式使用$(document).ready()

$(document).ready(function(){
    //your code here;
})

You can use the JavaScript pure load event in this way 您可以通过这种方式使用JavaScript纯负载事件

window.addEventListener('load', function () {
//your code right here;
}, false);

Source: Here 资料来源: 这里

If you want to suppori IE8: 如果要支持IE8:

document.onreadystatechange = function () {
    if (document.readyState == "interactive") {
        // run some code.
    }
}

Put the code you want to execute when the user initially loads the page in a DOMContentLoaded event handler like below: 将用户最初加载页面时要执行的代码放在DOMContentLoaded事件处理程序中,如下所示:

 document.addEventListener('DOMContentLoaded', function() { console.log('Whereas code execution in here will be deffered until the initial HTML document has been completely loaded and parsed, without waiting for stylesheets, images, and subframes to finish loading.'); }); console.log('This will log immediatley'); 

It's important to note that DOMContentLoaded is different than the load event 请务必注意, DOMContentLoadedload事件不同

translate.process() is asynchronous code which needs to make a call to a server and wait for its response. translate.process()是异步代码,需要调用服务器并等待其响应。 What it means is that, when you call this function, it goes in the background to go do its own thing while the rest of the page continues loading. 这意味着,当您调用此函数时,它会在后台执行自己的操作,而页面的其余部分继续加载。 That is why the user sees the page while this function is still running. 这就是为什么该功能仍在运行时用户看到页面的原因。

One minimal way I can think around this is by adding this to your css files in the head tag. 我可以考虑的一种最小方法是将其添加到head标签中的css文件中。

body { display: none }

And then, under this.process function, after the for loop ends, add 然后,在this.process函数下,在for循环结束之后,添加

document.body.style.display = 'block'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM