如何在页面加载时运行函数?
我想在页面加载时运行一个函数,但我不想在 标记中使用它。
我有一个脚本,如果我在 中初始化它,就会运行它,如下所示:
function codeAddress() {
// code
}
<body onLoad="codeAddress()">
但我想在没有 的情况下运行它
我已经尝试了很多东西,例如:
window.onload = codeAddress;
但它不起作用。
那么当页面加载时如何运行它呢?
I want to run a function when the page is loaded, but I don’t want to use it in the <body>
tag.
I have a script that runs if I initialise it in the <body>
, like this:
function codeAddress() {
// code
}
<body onLoad="codeAddress()">
But I want to run it without the <body onload="codeAddress()">
and I have tried a lot of things, e.g. this:
window.onload = codeAddress;
But it is not working.
So how do I run it when the page is loaded?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
window.onload = codeAddress;
应该可以工作 - 这里有一个演示,以及完整的代码:window.onload = codeAddress;
should work - here's a demo, and the full code:自 jQuery 发布以来,原生 JavaScript 不再使用 jQuery 或 window.onload,而是采用了一些很棒的功能。所有现代浏览器现在都有自己的 DOM 就绪功能,无需使用 jQuery 库。
如果你使用原生 Javascript,我会推荐这个。
Rather than using jQuery or window.onload, native JavaScript has adopted some great functions since the release of jQuery. All modern browsers now have their own DOM ready function without the use of a jQuery library.
I'd recommend this if you use native Javascript.
替代解决方案。我更喜欢这种方式,因为它简洁且代码简单。
这是一个匿名函数,未指定名称。
这里发生的是,函数是一起定义和执行的。
将其添加到正文的开头或结尾,具体取决于它是在加载页面之前还是在加载所有 HTML 元素之后立即执行。
Alternate solution. I prefer this for the brevity and code simplicity.
This is an anonymous function, where the name is not specified.
What happens here is that, the function is defined and executed together.
Add this to the beginning or end of the body, depending on if it is to be executed before loading the page or soon after all the HTML elements are loaded.
采用 Darin 的答案,但采用 jQuery 风格。 (我知道用户要求使用 javascript)。
运行小提琴
Taking Darin's answer but jQuery style. (I know the user asked for javascript).
running fiddle
window.onload = function() {
...等等不是一个很好的答案。这可能会起作用,但它也会破坏已经挂钩该事件的任何其他函数。或者,如果另一个函数在您的事件之后挂钩到该事件,它将破坏您的事件。
因此,您可能会在事后花费大量时间试图找出为什么以前有效的东西不再有效。
更可靠的答案此处:
我一直在使用的一些代码,我忘记了在哪里找到它以给予作者荣誉。
希望这有帮助:)
window.onload = function() {
... etc. is not a great answer.This will likely work, but it will also break any other functions already hooking to that event. Or, if another function hooks into that event after yours, it will break yours.
So, you can spend lots of hours later trying to figure out why something that was working isn't anymore.
A more robust answer here:
Some code I have been using, I forget where I found it to give the author credit.
Hope this helps :)
尝试 readystatechange
,其中状态为:
DOMContentLoaded
之前触发window.onload 之前触发
Try readystatechange
where states are:
DOMContentLoaded
window.onload
看一下 domReady 脚本,它允许设置多个函数在 DOM 加载时执行。它基本上是 Dom Ready 在许多流行的 JavaScript 库中所做的事情,但它是轻量级的,可以在外部脚本文件的开头进行获取和添加。
用法示例
Take a look at the domReady script that allows setting up of multiple functions to execute when the DOM has loaded. It's basically what the Dom ready does in many popular JavaScript libraries, but is lightweight and can be taken and added at the start of your external script file.
Example usage
window.onload 将像这样工作:
window.onload will work like this:
一旦页面加载,该函数就会运行:
或者:
As soon as the page load the function will be ran:
Alternatively:
我相信这是维持不同版本浏览器支持的最佳方式
I believe this is the best way to maintain support across different versions of browsers
通用跨浏览器网页加载器
我编写了一个 JavaScript 页面加载器,它应该可以解决您在页面加载后加载函数的问题。与其他帖子不同,此网页加载器99.9% 跨浏览器兼容,并且可以在许多版本的浏览器(新旧)中运行。支持几乎所有浏览器中加载页面,包括 Internet Explorer 3-11、所有 Firefox 和 Chrome 浏览器、早期 Opera、所有移动浏览器、Netscape 4 和 6 系列等。
它将选择最快的页面加载事件或状态检查给定的浏览器并返回一个文本字符串,指示 JavaScript 可以安全地处理文档对象模型 (DOM)。应该可以在许多旧版浏览器中工作,但需要测试。将您的 JavaScript 函数或或库调用放在下面的“Start()”方法中,这样一旦脚本显示网页或 DOM 在浏览器中加载,它们就会被触发。
作为旁注,我建议您将此代码放置在以下位置:
块中作为同步脚本,这会暂停页面以提前加载。
...或者...
标记文件中添加了“async”属性,以便它与您的页面并行安静地加载,但在下载完成时暂停 html 加载,以便它得到首先进行解析和处理。
如果使用这些方法,脚本不应过多阻塞渲染。您希望此脚本在首次构建网页 DOM 时准备好,而不是之后,特别是因为页面的后续状态可能会延迟等待图像、视频和 JavaScript API 下载。
注意:当您在 Web 浏览器中运行此脚本时,请务必按 F12 打开浏览器工具屏幕并检查控制台选项卡以查看结果。它会告诉您网页加载器在哪个阶段被触发以及何时调用“Start()”脚本。
在大多数现代浏览器(HTML5 或 2010 年后)中,应该在 HTML 标记的 DOM 或文档对象模型呈现后立即触发,但其余网页资源、CSS、图像、视频和其他文件仍在加载。在现代浏览器中,这通常处于“交互”和“完成”的就绪状态之间,并且 DOM 已构建,但浏览器仍在下载其他资源文件。这允许您的 JavaScript 很早就访问并开始操作 HTML 树或 DOM。
较旧的浏览器(例如 Internet Explorer v. 3-8 或 Netscape)不了解高级 DOM 检查,因此在调用 JavaScript 之前需要完全或“完全”加载 DOM 和所有页面资源。
Universal Cross-Browser Web Page Loader
I wrote a JavaScript page loader that should solve your issues loading a function after the page is loaded. This web page loader is 99.9% cross-browser compatible and works in many versions of browsers, old and new, unlike the other posts. Includes support for loading pages in nearly all browsers, including Internet Explorer 3-11, all Firefox and Chrome browsers, early Opera, all mobile browsers, Netscape 4 and 6 series, etc.
It will pick the fastest page load event or state check for a given browser and return a text string indicating JavaScript may safely process the Document Object Model (DOM). Should work in many legacy browsers, but test. Place your JavaScript functions or or library calls inside the "Start()" method below, so they are triggered as soon as the script says the web page or DOM is loaded in the browser.
As a side note, I recommend you place this code either:
<script>
block as a synchronous script, which pauses the page to load early....or...
<script>
tag file with the "async" attribute added so it loads quietly in parallel to your page but pauses html loading when download complete so it gets parsed and processed first.The script should not render-block much if using these methods. You want this script ready when the web page DOM is first built and not after, especially since later states of the page could get delayed waiting for images, videos, and JavaScript API's to download.
Note: When you run this script in a web browser, be sure to press F12 to pull up the browser tools screen and check the console tab to see the result. It will tell you at what stage the web page loader was triggered and when it called the 'Start()' script.
In most modern browsers (HTML5 or post-2010) it should be triggered as soon as the DOM or Document Object Model of HTML markup is rendered but the rest of the web page resources, CSS, images, video, and other files are still loading. In modern browsers this is usually between a readystate of "interactive" and "complete" and the DOM is built but the browser is still downloading other resource files. This allows your JavaScript to access and start manipulating the HTML tree or DOM very very early.
Older browsers like Internet Explorer v. 3-8 or Netscape, do not understand the advanced DOM checks so would require the full or "complete" load of the DOM and all page resources before calling your JavaScript.