Page 1 of 1

Javascript order of execution

Posted: Fri Nov 09, 2018 11:17 am
by stevedb
Hi there,
I have a couple of problems I'm trying to understand.

1) Where's the best place to put a js 'src' library tag that's specific to a particular form?
--If I add it to the HTML object that's using it I get a console 'ReferenceError' for the library usage in the HTML object the FIRST time I log in and go to that form, after that it is fine.
--If I add the library to the 'Header' in the setup it seems to work, but I'm wondering if this is the 'right' solution
--I'd like to understand this so I don't keep hitting it in the future :geek:

2) I create a string in PHP in a forms 'Before Edit' event, at the end of the PHP I pass this string to a JavaScript variable with nuAddJavascript, but the HTML object that uses the variable gets a 'ReferenceError' the first time I enter the edit form.

They seem like related issues? Any guidance is appreciated!

Re: Javascript order of execution

Posted: Fri Nov 09, 2018 12:26 pm
by stevedb
I think I have got around issue (2).
I put the HTML element JavaScript inside a jquery document ready function ($(document).ready(function()).
I don't know why I need to do this though as the nuAddJavascript documentation says it 'Adds Javascript to the beginning of a Form's Javascript before it is run'. So I thought it would be one of the first things to run.
I'm no JS expert so that may be my miss-understanding of how JS executes on a web page.

Happy to be enlightened!

Re: Javascript order of execution

Posted: Fri Nov 09, 2018 9:55 pm
by admin
stevendb,
If I add the library to the 'Header' in the setup it seems to work, but I'm wondering if this is the 'right' solution
-
That's the way to do it.


Steven

Re: Javascript order of execution

Posted: Mon Nov 12, 2018 1:47 pm
by stevedb
Thanks,adding it to the header is fine.

The edit form is loaded before the 'Before Edit' and form's JS runs. This was my fundamental misunderstanding.
So to pass values from PHP to HTML elements on the edit form I need to pass the value as I am in the BE event, then specifically update the HTML element's value in the forms JS.
Thanks.

Re: Javascript order of execution

Posted: Mon Nov 12, 2018 10:31 pm
by admin
.