How to solve $ is not defined | Community
Skip to main content
Level 3
December 21, 2020
Solved

How to solve $ is not defined

  • December 21, 2020
  • 4 replies
  • 5208 views

I have a js function using $ in it.

My function uses $get(). and works fine when this function is called from HTL but if I add the function in document.load 

$(document).load(function () {
    alert("alert");
});

then I get $ is not defined in console.

Only the function which is called  in HTL(Sightly) works.

I'm using AEM 6.3.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Suraj_Kamdi

@vinit_pillai 

First thing, Jquery should always load before the execution of your code.

Second thing, It happens mainly because of your clientlib category /dependencies order.

4 replies

Asutosh_Jena_
Community Advisor
Community Advisor
December 21, 2020

@vinit_pillai You need to make sure the jquery is defined before this line of code is executed.

Level 3
December 21, 2020
Sorry If I don't underdstand this I'm new. Can you please elaborate on how can I make sure it is defined?
Anudeep_Garnepudi
Community Advisor
Community Advisor
December 21, 2020

Hi @vinit_pillai 

In the page where you are getting error, right click on the page and click view source and in source search(Ctrl+F) for jquery. Make sure that jquery loads in head section of your page.

document. is Javascript and will work without error

$(document). $ is jquery framework, we should load it before using it on page.

-AG 

AG
Suraj_Kamdi
Community Advisor
Suraj_KamdiCommunity AdvisorAccepted solution
Community Advisor
December 21, 2020

@vinit_pillai 

First thing, Jquery should always load before the execution of your code.

Second thing, It happens mainly because of your clientlib category /dependencies order.

February 28, 2022

If you are using any script file and getting "Uncaught ReferenceError: x is not defined " which means ‘x’ is either a variable or a method which you are trying to use before declaring it using var keyword. This means that there is a non-existent variable referenced somewhere. This variable needs to be declared, or you need to make sure it is available in your current script or scope otherwise , it will endup throwing this ‘x’ is not defined error . This usually indicates that your library is not loaded and JavaScript does not recognize the ‘x’.

To solve this error: Load your library at the beginning of all your scripts.

There can be multiple other reasons for this issue:

  • Conflict with Other Libraries
  • Path to your library included is not correct
  • Llibrary file is corrupted
  • Working offline (when you use CDN)