onload之前的Javascript? [英] Javascript before onload?

查看:328
本文介绍了onload之前的Javascript?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在onLoad / onPageShow之前是否有任何事件处理程序? onLoad的问题是如果显示有任何变化,页面将在完全加载之前显示而不会发生变化,然后脚本将运行。确保它尽快运行的最佳方法是什么?

Is there any event handler before onLoad/onPageShow? The trouble with onLoad is if there is any change in display, the page will show up without change until it is fully loaded, then the script will run. What is the best way to make sure it will run as soon as possible?

推荐答案

如果你把Javascript 语句(而不是函数定义)在< script> 标记内,它们将在页面加载期间执行 - 在onLoad事件被触发之前。

If you put Javascript statements (rather than function definitions) inside a <script> tag, they will be executed during page loading - before the onLoad event is fired.

 <html>
 <body>
   <h2>First header</h2>
   <script type="text/javascript">
     alert("Hi, I am here"); 
     document.write("<h3>This is Javascript generated</h3>");
   </script>
   <h2>Second header</h2>
 </body>
 </html>

需要注意的是,您无法按ID搜索元素,因为这些元素可能尚未呈现,所以你以这种方式改变页面的能力是有限的。

The caveat is that you cannot search for elements by ID because these element might not have been rendered yet, so your ability to change the page that way is limited.

底线:可能,不推荐。

我通常在做什么这种情况如下:

What I usually do in such situations is as follows:


  • 使页面中可能变化的部分不可见(通过 style =visibility :隐藏;);

  • 让onLoad运行更改页面的Javascript代码,然后将所述部分的可见性设置为 visibility:visible

  • Make the parts of the page that may change invisible (via style="visibility:hidden;");
  • Make onLoad run a Javascript code that changes the page and then sets the visibility of the said parts to visibility:visible.

这篇关于onload之前的Javascript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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