HTML 中的 Javascript 加载

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3870185/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 06:26:16  来源:igfitidea点击:

Javascript onload in HTML

javascripthtmljsp

提问by Questions

I want to ask a question about the Javascript's onload.

我想问一个关于 Javascript 的问题onload

I'm writing a JSP page with the code <%@ include file ="body.jsp". The included body.jspcontains:

我正在用代码编写一个 JSP 页面<%@ include file ="body.jsp"。包含的body.jsp内容包括:

<table onload="function()">

This shouldload the javascript function, but it doesn't appear to have any effect on the page. Is onloadonly usable on the bodytag?

应该加载 javascript 函数,但它似乎对页面没有任何影响。是onload仅在使用body标签?

回答by gblazex

Onload can only be used for <body>, <img>, <script>, <iframe>tags, because it tells you when an external resource (image, script, frame) or the whole page (body) has been loaded

Onload 只能用于<body>, <img>, <script>,<iframe>标签,因为它会告诉您何时加载了外部资源(图像、脚本、框架)或整个页面(正文)

Since HTML5these can also fire a loadevent: <link>, <style>, <input type=image>, <object>
Support for these can still be a hit or miss though (e.g. older Android browsers)

由于HTML5这些也可以触发加载事件:<link>, <style>, <input type=image>,<object>
尽管如此,对这些的支持仍然可能是命中或未命中(例如旧的 Android 浏览器

回答by Marko

Why not just include it via a <script tag>?

为什么不直接通过 a 包含它<script tag>

Inside your .jsp file

在您的 .jsp 文件中

<script>
    window.onload = function() {
        alert("Hello!");
    }
    // or to execute some function
    window.onload = myFunction; //notice no parenthesis
</script>

回答by philgiese

As the other guys already stated the onLoad event will not fire on a table. What you can do ist attaching the onLoad-handler to the body element (which will then fire, when the page is loaded) and manipulate the table by for example assigning an id to the table.

正如其他人已经说过的 onLoad 事件不会在桌子上触发。您可以做的是将 onLoad-handler 附加到 body 元素(然后在页面加载时触发)并通过例如为表格分配一个 id 来操作表格。

<body onload="function() { var table = document.getElementById("table-id"); ... }">
    <table id="table-id"></table>
</body>

Are you using some javascript framework?

你在使用一些 javascript 框架吗?

回答by Thariama

"onLoad" may be used on body- and frameset-tags. To see some action you may use:

“onLoad”可用于正文和框架集标签。要查看您可以使用的某些操作:

<body onload="function(){alert('This is an action!')}">

回答by pangolin

The easiest way i find is to use an external javascript file and jquery.

我找到的最简单的方法是使用外部 javascript 文件和 jquery。

// Variables and functions you want to declare 
var socket = io.connect();
// .....

// Function you want to run on load
$(function() {
    $('#submit').click(function() {addUser();});
    // ... any other functions you want to run on load
});

This is a code snippet from something that i was working on. The variable is declared before the code runs (It creates a web socket). Then there is the jquery document selector ($) which runs on load and calls the init function to modify my html. I use it to call an anonymous function which runs right away.

这是我正在研究的代码片段。该变量在代码运行之前声明(它创建了一个网络套接字)。然后是 jquery 文档选择器 ( $),它在加载时运行并调用 init 函数来修改我的 html。我用它来调用一个立即运行的匿名函数。

回答by CTS_AE

You can throw a <script>tag right after your table with code. Once it gets to the script tag it would mean that the DOM for the table element above it has been loaded and can now be accessed in your script below it.

您可以<script>在带有代码的表格之后立即抛出一个标签。一旦它到达 script 标签,就意味着它上面的 table 元素的 DOM 已经被加载,现在可以在它下面的脚本中访问。

Note: The following below isn't applicable to the question but rather the other answers being given.

注意:以下内容不适用于该问题,而是给出了其他答案。

I recommend using the addEventListenerfunction in javascript for adding the event. This makes sure that you are not overwriting or going to be overwritten by anyone else wanting to listen to the event.

我建议使用addEventListenerjavascript 中的函数来添加事件。这可确保您不会被任何其他想要收听该事件的人覆盖或将其覆盖。

Example

例子

var iframe = document.getElementsByTagName('iframe')[0];
iframe.addEventListener('load', function(event){ console.log("iframe Loaded", event); })