Home > Software engineering >  [ODOO15]Execute a JavaScript script when a page is fully loaded
[ODOO15]Execute a JavaScript script when a page is fully loaded

Time:08-31

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.

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. I select the button through its class, but the returned object remains empty.I'm simply doing something like :

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.

How could I execute JavaScript code only when my page is fully loaded, so I can be sure that all of my elements exist?

Thank you, Regards,

CodePudding user response:

Try to use DOMContentLoaded event for all your script.

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

Example:

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

CodePudding user response:

How about using $(document).ready()?

Example:

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

See more about ready on jQuery API Documentation.

  • Related