我们可以向用户隐藏javascript加载吗? [英] Can we hide javascript loading from the user?

查看:68
本文介绍了我们可以向用户隐藏javascript加载吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个页面,该页面是使用PHP生成的,然后是使用JS修改的页面,则该页面使用:

If I have a page which gets generated using PHP and then modified using JS which runs using:

window.onload=myFunction;

然后用户看到原始页面,然后该页面似乎随着js代码的运行而四处移动,并将其自己的位添加到页面上.

Then the user sees the original page, which then appears to flick around as the js code gets run and adds its own bits onto the page.

是否有一种方法可以避免用户看到此过程,或者至少使其对他们不那么明显?也许比window.onload(一种可以在下载图像等开始工作的方法)更合适的方法来加载javascript?

Is there a way to avoid the user being able to see this process, or to at least make it less obvious to them? Maybe there is a more suitable method of loading the javascript than window.onload (one that can begin to work as the images etc. are downloading)?

推荐答案

我建议首先在主体上设置一些类,以切换某些元素的可见性.

I would recommend first settings some class on the body which will toggle the visibility of some elements.

<body class="loading"> .... </body>

然后,当脚本完成加载时,您可以删除此类并显示所需的所有内容.您甚至可以使用加载"类显示一些精美的背景,例如Mozilla中点状旋转的圆圈.

Then, when the script finishes loading, you can remove this class and show all the things you need. You can even use the "loading" class to show some fancy background, like a spinning circle of dots in Mozilla.

有些项目还使用另一种加载脚本的方法,而不是"onLoad".首先,创建一个名为"domReady"的全局数组.

Also some projects use another method of loading scripts instead of "onLoad". First you create a global array, let's say, named "domReady".

window.domReady = [];

当您需要添加一些将在页面加载时运行的功能时,只需将其推入此数组即可:

And when you need to add some function that will be run on page load, you just push it into this array:

window.domReady.push( function foo(){ alert('Hey!'); } );

然后在文档的最后一行放置将运行所有推送过程的函数:

And at the last line of the document you put the function that will run all the pushed procedures:

for (procedure in window.domReady) {
    window.domReady[procedure].call();
}

由于它将是文档结束标记之前的最后一行,因此DOM将准备就绪,您不必等到所有图像都加载完毕.

Since it will be the last line of the document before the closing tag, the DOM will be ready, and you won't have to wait until all the images are loaded.

这篇关于我们可以向用户隐藏javascript加载吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆