简体   繁体   English

[ODOO15]当页面完全加载时执行 JavaScript 脚本

[英][ODOO15]Execute a JavaScript script when a page is fully loaded

I'm working on a JavaScript script that aims to add some events on a couple of buttons in the barcode interface in Odoo V15.我正在开发一个 JavaScript 脚本,该脚本旨在在 Odoo V15 的条形码界面中的几个按钮上添加一些事件。

When I'm trying to add an event on a button in the standard navbar at the top of the page (the navbar that allows, for example, to go back to the applications list) I can't locate the button with jQuery.当我尝试在页面顶部的标准导航栏中的按钮上添加事件时(例如,允许将 go 返回应用程序列表的导航栏)我找不到带有 jQuery 的按钮。 I select the button through its class, but the returned object remains empty.I'm simply doing something like:我 select 通过它的 class 按钮,但返回的 object 仍然是空的。我只是在做类似的事情:

console.log($('.buttonClass'));

I guess that is because my script executes before the button generation.我想这是因为我的脚本在按钮生成之前执行。 I tried to place my script at the last position of the assets in the manifest, but it still not working.我试图将我的脚本放在清单中资产的最后一个 position 处,但它仍然无法正常工作。

How could I execute JavaScript code only when my page is fully loaded, so I can be sure that all of my elements exist?只有当我的页面完全加载时,我如何才能执行 JavaScript 代码,这样我才能确定我的所有元素都存在?

Thank you, Regards,谢谢, 问候,

Try to use DOMContentLoaded event for all your script.尝试对所有脚本使用 DOMContentLoaded 事件。

More here: https://developer.mozilla.org/en-US/docs/Web/API/Window/DOMContentLoaded_event更多信息: https://developer.mozilla.org/en-US/docs/Web/API/Window/DOMContentLoaded_event

Example:例子:

 addEventListener('DOMContentLoaded', (event) => { // your code console.log($('.buttonClass')); });

How about using $(document).ready()?使用 $(document).ready() 怎么样?

Example:例子:

 $(document).ready(function() { console.log($('.buttonClass')); });

See more about ready on jQuery API Documentation .jQuery API 文档上查看有关ready的更多信息。

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

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