简体   繁体   English

启动 onclick 比使用 document.onload 更快

[英]Initiate onclick faster than with document.onload

I have html-pages with links where i want to attach a function to their onclick event.我有带有链接的 html 页面,我想在其中将函数附加到他们的onclick事件中。 One way to do it is of course:一种方法当然是:

<a href="save.php" onclick="save(); return false;" rel="save">Save</a>

But I know this is not the best practice.但我知道这不是最佳做法。 So instead I wait for window.onload , loop through the links and attach the save-function to the links with rel="save" .因此,我等待window.onload ,遍历链接并将保存功能附加到带有rel="save"的链接。 The problem with this is that it waits until the whole page has finished loading which can be several seconds after the link is displayed and clickable.这样做的问题是它会等到整个页面完成加载,这可能是链接显示和可点击后的几秒钟。

So is there another way to do this?那么还有其他方法可以做到这一点吗? Avoiding onclick in the html but that makes it work immediately when the link is rendered.避免在 html 中点击onclick ,但这使得它在呈现链接时立即工作。

Internet Explorer has a handy attribute for <script> tags called defer . Internet Explorer 为 <script> 标记提供了一个方便的属性,称为defer It's for delaying the parsing of a script until the document has finished loading.它用于延迟脚本的解析,直到文档完成加载。 For other browsers that support it, you can use DOMContentLoaded, as someone else suggested and for browsers that don't support either you can fall back to onload.对于支持它的其他浏览器,您可以使用 DOMContentLoaded,正如其他人所建议的那样,对于不支持这两种浏览器的浏览器,您可以回退到 onload。

<script type="text/javascript" defer>
//- Run this code when the DOM parsing has completed
</script>

I did a quick Google search for "DOMContentLoaded defer" and found the following page that might help:我在 Google 上快速搜索了“DOMContentLoaded defer”,发现以下页面可能会有所帮助:

http://tanny.ica.com/ica/tko/tkoblog.nsf/dx/domcontentloaded-event-for-browsers http://tanny.ica.com/ica/tko/tkoblog.nsf/dx/domcontentloaded-event-for-browsers

You could try DOMContentLoaded event instead of load .您可以尝试DOMContentLoaded事件而不是load IE also gives you the defer attribute for script tags, which defers execution until the DOM is loaded. IE 还为脚本标签提供了 defer 属性,该属性会延迟执行,直到加载 DOM。 If those don't work for you, then you are stuck with the solutions you mention, as far as I know.据我所知,如果这些对你不起作用,那么你就会被你提到的解决方案所困扰。

In your case, you can just leave that as it is.在您的情况下,您可以保持原样。 Stick to the simplest possible thing, even if it is not the general best practice.坚持尽可能简单的事情,即使这不是一般的最佳实践。

I don't know if this is appropriate for your solution, but you could insert script immediately below the are with the links you need altered.我不知道这是否适合您的解决方案,但您可以在下面立即插入脚本,其中包含您需要更改的链接。 This script would not be wrapped in a function, allowing the browser to execute it immediately when seen.这个脚本不会被包装在一个函数中,允许浏览器在看到它时立即执行它。 The effect is that you can run script before the full page is loaded, altering only the items that exist above the script being run.效果是您可以在加载整个页面之前运行脚本,只更改正在运行的脚本上方存在的项目。 (If you reference something below the script, it will fail.) (如果您在脚本下方引用某些内容,它将失败。)

BTW, this is almost certainly not a best practice, and some would probably label it a worst practice.顺便说一句,这几乎肯定不是最佳实践,有些人可能会将其标记为最差实践。

How about this?这个怎么样?

<a href="javascript:void(save());">Save</a>

Note: This solution requires to users to have Javascript enabled.注意:此解决方案要求用户启用 Javascript。 Not exactly best practice, but may be suitable for your scenario.不完全是最佳实践,但可能适合您的场景。

The ideal here would be to use the ideas of Unobtrusive Javascript.这里的理想是使用 Unobtrusive Javascript 的思想。

In this way, if the Javascript isn't loaded the link would still do something.这样,如果未加载 Javascript,链接仍然会执行某些操作。 It's a link right, so it leads the user to another piece of content?这是一个链接,所以它会将用户引导到另一条内容? - this should work without Javascript. - 这应该在没有 Javascript 的情况下工作。 And if the functionality attached to the links can ONLY work with Javascript you should create and insert them into the DOM with Javascript (they aren't clickable if they aren't there...).如果附加到链接的功能只能Javascript 一起使用,您应该使用 Javascript 创建并将它们插入到 DOM 中(如果它们不存在,则它们是不可点击的......)。

(Otherwise how about delegating the click event to a wrapper element? Does that work before the element is complete?) (否则如何将点击事件委托给包装元素?这在元素完成之前有效吗?)

edit: Oh, and "save" sounds very much like it ought to be a button in a form rather than a link.编辑:哦,“保存”听起来很像它应该是表单中的按钮而不是链接。 The Unobtrusive JS stuff still applies though.不过,Unobtrusive JS 的东西仍然适用。

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

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