Current year JS not displaying on the app

Current year JS not displaying on the app

by Javier Tejera -
Number of replies: 13

Hi there,

I am trying to display the current year on a page using this JS code:

<script>document.write(new Date().getFullYear())</script>
It works on the browser, but on the app it is just a blank space. Any idea why it doesn't work?

Thanks
Average of ratings: -
In reply to Javier Tejera

Re: Current year JS not displaying on the app

by Dani Palou -
Picture of Core developers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Hi,

IIRC there is no way to add a script from a piece of HTML, even if it's an inline script. I think in most cases it will be filtered by Moodle before returning to the app, and even if it reaches the app I think the script won't be loaded. Simple things like adding an onclick to an HTML can be added (if Moodle doesn't filter them), but not scripts. I tried it some time ago so I might be wrong, but I think this is what happened.

Cheers,
Dani
In reply to Dani Palou

Re: Current year JS not displaying on the app

by Javier Tejera -
Hi Dani, thanks for your reply. Do you think there is an alternative? We need to update this on an yearly basis and it's a headache given that we have it in hundreds of pages!

Javier
In reply to Javier Tejera

Re: Current year JS not displaying on the app

by Dani Palou -
Picture of Core developers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Right now the only alternative I can think of to execute JS in the app is to use a plugin adapted to the app, but that would be too hacky because it means the plugin would modify other pages in the app so I don't recommend it.

Depending on where it's applied maybe you can do something in PHP. The current year would be calculated in PHP and applied to the template or wherever it's needed. But this would only work if the app renders the same HTML as Moodle LMS, like in mod_page or mod_book, and it should receive the year already applied.
In reply to Dani Palou

Re: Current year JS not displaying on the app

by Pep Mulet Pol -
Hi Dani and Javier,

I am currently facing the same problem. That is to say, we have some javascript code that runs flawlessly in the browser, and it is completely ignored by Moodle app.

I have tried to inject the javascript code through a filter plugin using
$PAGE->requires->js(new moodleurl("url_of_the_js");
but I get the same outcome: It works on browser, but it doesn't on mobile app.

The possibility of using a filter was inspired by the Mathjax filter which displays mathematical formulas. Surprisingly, this filter does its job on moodle app and I don't understand why, since I am using the same approach.

https://github.com/moodle/moodle/blob/master/filter/mathjaxloader/filter.php

Meanwhile, have you found some workaround to his problem?

Thanks,
Josep
In reply to Pep Mulet Pol

Re: Current year JS not displaying on the app

by Dani Palou -
Picture of Core developers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Hola Josep!

The MathJax filter works because it has Javascript code in the app to make it work.

You will need to adapt your filter plugin to work in the app, you can check this guide (and this is the same guide in the old wiki). Unfortunately there are no examples for filters, I don't know if there's any filter out there that has been adapted to run some JS in the app. Basically you will need to return some Javascript code to register a handler in CoreFilterDelegate. In this link you can find the interface your handler needs to implement.

Cheers,
Dani
In reply to Dani Palou

Re: Current year JS not displaying on the app

by Pep Mulet Pol -
Hi, Dani
Thank you very much for the guides. I have read them, and now I have a better understanding of how moodle app works. However, none of the examples and adapted plugins are filters, so it is quite difficult to figure out how to put things together without a complete example.

At the moment, It is not crucial to have this feature working on mobile app. I was concerned about the fact that we are inserting [script][/script]  at the end of some pages of the teacher's books in order to create dynamic components. I am worried that these scripts could lead to incompatibilities which make our materials unusable in the moodle app. In fact, we already have some sort of incompatibility related to the usage of Bootstrap accordion that does not work on the app.

Instead of using [script][/script] , could we create the components in an external HTML page and insert them as iframe in the book? This should work in the moodle app, right?

Best wishes,
Josep
In reply to Pep Mulet Pol

Re: Current year JS not displaying on the app

by Javier Tejera -
Hi Josep, not sure if this will help, but we also use quite a bit of Bootstrap simple components (e.g. badges and labels) and what we are doing as a workaround is adding the Bootstrap CSS code to the mobile remote theme and so they work the same in both web and app.
In reply to Javier Tejera

Re: Current year JS not displaying on the app

by Pep Mulet Pol -
Hi Javier,
This would definitively help. However, my institution has not purchased a Premium subscription yet and the mobilecssurl does not work in free mode. I suppose that It would have sense to buy it if we could fix the problem with custom JS.

Moreover, the BS accordion component uses javascript besides css, so it does not respond to clicks in mobile app.
Thanks,
Josep
In reply to Pep Mulet Pol

Re: Current year JS not displaying on the app

by Dani Palou -
Picture of Core developers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Hi Josep!

Yes, iframes should work fine in the app. The only problem about using iframes is that they will only work online, if the user is offline he'll be able to see the content outside of the iframe (if he previously downloaded or opened the book) but the iframe will be empty because the app won't be able to connect to the server.

Cheers,
Dani
In reply to Dani Palou

Re: Current year JS not displaying on the app

by Pep Mulet Pol -
Hi Dani,
Perfect! Bearing in mind the problem associated with JS in mobile app, I would prioritize iframes over JS scripts in our books. I understand the issue of a device being offline.
Cheers,
Josep
In reply to Pep Mulet Pol

Re: Current year JS not displaying on the app

by Javier Tejera -
Thought about that, but most of my users access offline... 
In reply to Pep Mulet Pol

Re: Current year JS not displaying on the app

by Javier Tejera -
Hi Pep,

I couldn't manage to do it. If you end up discovering a workaround please feel to share it! It would be much appreciated.

Javier