Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Getting error : "Uncaught TypeError: AJS.$ is not a function"

Saurabh Katiyar April 21, 2016

Hello,

I am using JIRA 6, I added a custom javascript file in following jsp files :  

  • <atlassian-jira>/includes/decorators/aui-layout/head-common.jsp

  • <atlassian-jira>/includes/decorators/aui-layout/head-common-nodecorator-pre.jsp

this file contains some common functions that i want within every JIRA page. When loading page in browser console shows "Uncaught TypeError: AJS.$ is not a function" on following line :

AJS.$(document).ready(function() {

  << Some custom code here>>

});

 

It appears like JIRA is loading custom js file before AUI & jquery. ( If it may be a possibility then Is there any solution for this? )

any suggestions are welcome!

2 answers

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

1 vote
Sean Curtis
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
April 25, 2016

I would recommend against injecting code into JIRA's jsp files — use the web resource context that @Volodymyr Krupach mentioned and add a dependency on <dependency>com.atlassian.plugins.jquery:jquery</dependency> to ensure jQuery is loaded on the page first.

However if you're going to be adding code to jsp files, then in you'd be better off adding it to the bottom of /atlassian-jira/includes/decorators/aui-layout/footer.jsp

0 votes
Volodymyr Krupach
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 21, 2016

Do not do changes to JIRA JSPs.

I guess you want your JS to work on all JIRA pages so you can wrap an addon with web-resource. Your web-resource should look like:

&lt;web-resource name="Resources" key="resources"&gt;
  &lt;resource name="foo.js" type="download" location="resources/foo.js"/&gt;
  &lt;dependency&gt;com.atlassian.auiplugin:ajs&lt;/dependency&gt;
  &lt;context&gt;atl.general&lt;/context&gt;
  &lt;context&gt;atl.admin&lt;/context&gt;
&lt;/web-resource&gt;
Saurabh Katiyar April 21, 2016

Thanks Volodymyr,

Yes i want my js on all pages (view + edit + create screens).

It was working earlier in JIRA 5, you may include custom js file in <atlassian-jira>/atlassian-jira/includes/decorators/aui-layout/header.jsp which has been changed in JIRA 6.

I did follow this kb article for JIRA 6.


Volodymyr Krupach
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 21, 2016

Hi Saurabh,

Did you try put the script to the end of head-common.jsp and head-common-nodecorator-pre.jsp?

Have you restarted JIRA after you changes?

Saurabh Katiyar April 21, 2016

Yes I did that but still getting the same error ("AJS.$ is not a function").

Volodymyr Krupach
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 21, 2016

Looks through the page source to find where your script is injected. Also you may check if jQuery is already inited and use it instead of AJS.$.

Saurabh Katiyar April 21, 2016

I did tried that too, it throws "ReferenceError: jQuery is not defined". i think its probably because custom js file is being load before jquery & AJS.

Volodymyr Krupach
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 21, 2016

Maybe you can go with pure JS script "document ready" approach without jQuery: http://stackoverflow.com/questions/799981/document-ready-equivalent-without-jquery

Stephen Deutsch
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 22, 2016

Maybe you should also try 

AJS.toInit(function() {
  //Code here
});
Saurabh Katiyar April 22, 2016

Thanks Stephen for your reply, 

AJS.toInit will not work (I did tried that too) because AJS & jQuery are not available in context when cutom js is being execute.

 

 

TAGS
AUG Leaders

Atlassian Community Events