Javascript not running in my app

I installed an app, and I’m playing with javascript a little bit but something is not really working as expected. If I add the following and visit my modules page, which has plenty or links, it doesn’t work:

$('a.module-section-link').css("color", "red")

However, the same runs pretty well inside the console. None of these work either:

$(document).on('app_ready', function() {
    $('a.module-section-link').css("color", "red")
$(document).ready(function() {
    $('a.module-section-link').css("color", "red")

Do you know what am I doing wrong please?

Please check your logs in browser

@johnskywalker I did that but logs are clean… No warning or error is returned.

have you tried reloading/clear cache? or try stopping bench and do bench build

Of course I did all that! I’m currently running bench start and I can see my javascript being built and it gets processed correctly. It’s there and the browser runs it… It only just doesn’t work!

That’s puzzling because I don’t understand what works and what doesn’t and why :pensive:
You mean it works for you?

You are making your own javascript file right? Where did you put it?

Made a new app, installed it, added this to my hooks:

app_include_js = "/assets/newapp/js/scripts.js"

and build.json:

    "newapp/js/scripts.js": [

The script is in the js folder in apps and correctly built in sites. The browser does load it, it’s there, I can see it in chrome developer tools as well. It just doesnt do anything.

I’m pretty sure that the script (at least on document ready) should work. The script runs just fine in the console, which as far as I can get, it means only one thing: it’s not ready yet by the time it runs.

So how am I supposed to run custom scripts inside a new app?

What does your code do by the way? is it supported by your browser?

It does exactly that! A cool thing… turns anchors red :sunglasses: I’m just trying to play around with ERPNext so far and getting to know it. Are you able to make that simple thing work? I just wanna know what it is that I’m doing wrong. The docs tell me that all javascript included that way should run just fine, but it doesn’t.

My hunch is that I just ignore some callback or trigger in place to activate my code and I just don’t know what it is. Do you?

Exactly, I think I am right, in fact this works (thanks @johnskywalker to let me talk and think :slight_smile:):

var checkExist = setInterval(function() {
   if ($('a.module-section-link').length) {
      $('a.module-section-link').css("color", "red")
}, 100); // check every 100ms

Can anybody please help showing the right way to include scripts in custom apps?

I see this is great :slight_smile: yup I was able to integrate js camera, and js barcode. Have tried putting console.log just to make sure that this is called?

:grin: Yep!
But it’s just not the way to go. I know there must be some callback function for the app to be ready. Tried all event listeners I could find but actually had no luck so far. Do you have an answer @johnskywalker?

@johnskywalker relax… I tell you, you just can’t do that kind of things. The dom is frozen, so these very simple things are just not possible.

1 Like

@imapirate Hi, so this is solved already right? :smiley:

If you accept it to not being possible as a solution… yeah, I think that’s the end of the line as of today :pensive:

This topic was automatically closed after 24 hours. New replies are no longer allowed.