Search code examples
headerodoo

(Odoo) Include CSS and JavaScript files on a single page


My question is how can I include/import a couple of JavaScript and CSS files in the header of a single page. I know that you can include the files in the web.assets_frontend or web.assets_backend but that causes those scripts and links to appear on every page across the frontend or backend part of Odoo. This causes the downgrade of the speed of the website.

Thanks


Solution

  • Reject a Deferred object and call any fail Callbacks with the given args.

    In your js files, add the following inside your "odoo.define" and right after your "require" statements:

    if(!$('.attachment_static').length) {
        return $.Deferred().reject("DOM doesn't contain'.attachment_static'");
    }
    

    This will only execute the js if it finds any html element with the class "attachment_static" when loading any page of the website