Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
3 Trả lời
21335 Lượt xem

Hello, I am trying to write some JQUERY code in a .js file. No matter how is write the code I get a

ReferenceError: $ is not defined

I know this is related to jquery and I looked at other .js file and they seem to reference $ just fine. What I am missing? Even this errors out


(function () { 

$(document).ready(function() {

});

}());


Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

Do you need the () at the end?

Try:

(function () {
 $(document).ready(function() {
});

});

Ảnh đại diện
Huỷ bỏ
Tác giả

Shawn this seems to work but I think my problem goes deeper. My code requires jquery so I need to put it at the bottom after all the other JavaScript files, would you you know how to do that?

If I understood your question correctly, I think Nedas's answer below would address it. If you could let us know more specifically what you need, we could explain better with code snippets.

Câu trả lời hay nhất

Hello,

I am encountering an issue in Odoo 18 while working with JavaScript files for a website theme. Specifically, the plugins are throwing errors such as 'x' is not defined and similar issues.

Although jQuery (version 3.6.3) is included in the web module/package and is loaded by default in the web.assets_frontend bundle, the errors persist even when using plugins compatible with this version of jQuery.

Here are the approaches I have tried in the web.assets_frontend bundle within the __manifest__.py file:

  1. Attempted to load the assets without explicitly including jQuery, as the required plugins are already compatible with version 3.6.3.
  2. Used a direct CDN link for jQuery.
  3. Provided a local path to jQuery in the asset declaration.

My primary goal is to implement animated snippets on the website. However, despite trying the above approaches, I have been unable to resolve the issue.

I would greatly appreciate any insights or solutions to overcome this challenge.

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

Hi Ralph,

This might mean that you include your file before the jQuery is loaded.

To make sure you include your file after jQuery library you would need to extend either "website.assets_frontend" or "website.assets_backend" depending where you use it.

See example inheritance of "website.assets_frontend" template below.

<template id="assets_frontend" inherit_id="website.assets_frontend">
    <xpath expr="." position="inside">
        <script type="text/javascript" src="/module_name/static/src/js/filename.js"/>
    </xpath>
</template>

The code above extends "website.assets_frontend" and includes new JS file to the bottom (position="inside") of the list meaning all other JS files will be loaded before this.

Ảnh đại diện
Huỷ bỏ

I am also facing the same error on loading my template. I have added my JS file in the 'assets_frontend' like you said, but it didn't resolve the problem.

Bài viết liên quan Trả lời Lượt xem Hoạt động
2
thg 12 24
888
0
thg 5 23
3087
0
thg 9 22
1378
2
thg 7 22
11902
0
thg 6 20
2375