Dev Machine
Dev Machine

Reputation: 117

(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

Upvotes: 0

Views: 569

Answers (2)

Franyer Hidalgo
Franyer Hidalgo

Reputation: 26

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

Upvotes: 1

Saumil gauswami
Saumil gauswami

Reputation: 725

you can use the <script> tag If you are talking about <form> tag in backend view, here is an example for you.

<form string="Product Collection">
    <group col="4">
       <field class="my_field" name="name"/>
    </group>
</form>
<script>
    var my_field = $('.my_field')
    console.log(my_field)
</script>

It'll only load this JavaScript when your form view is loaded.

Upvotes: 0

Related Questions