Including Javascript

Questions related to customising nuBuilder Forte with Javascript or PHP.

Including Javascript

Unread postby icoso » Thu May 20, 2021 1:00 am

In reference to a previous couple of posts.

viewtopic.php?f=20&t=10850
and this post on page two:
viewtopic.php?f=20&t=10320

Can I access or load an external Javascript file from within the Custom code of an object?

The reason I'm asking about this is because I found that on a launch form that I have, I have a button that will run some code to erform an SQL search and then call another form to display the results. I found that the Custom code that is allowed seems to be limited in length. As I updated and added code to my Custom code on my button object, the bottoms rows were getting cut off. SO then I thought I would try to move the bulk of my javascript to an external file and I added the following code to the beginning of the Custom Code

Code: Select all
<script src="'myjsfile.js" type='text/javascript'></script>


Then when I clicked save I lost my form. I mean it actually disappeared from the forms list. Which means I lost all the code I had in that form. I checked the zzzzsys_object table and could see all of my objects that I had on that form were still there but the form was gone. I checked the zzzzsys_form table and it was gone from there too. So then I copied one of my existing forms and I changed the form_id to the same ID that my lost form had. When I then opened that form none of my objects were showing up. Upon further inspection of the bject list, I seen another ID field for the sob_all_zzzzsys_tab_id field. Then scanning thru the zzzsys_tab table that ID was nowhere to be found as well. so I again copied an existing record, changed the tab_ID and the form_id to the appropriate ID's adn now I got my form back and the objects and I have the code that was on my objects (including the button) but I still lost my code on the form itself. WOuld I find that original form anywhere else in the tables?

The only thing I can correlate to losing the form was the way I typed in the <script src...> tags. I used double quotes around that file name instead of single quotes. Could this have caused the loss of the form?
icoso
 
Posts: 173
Joined: Mon Feb 08, 2021 7:39 am
nuBuilder Version: 4.5

Re: Including Javascript

Unread postby kev1n » Thu May 20, 2021 2:00 am

Add a reference to you external JS file in nuconfig.php:


Code: Select all
$nuConfigIncludeJS               = '';   //-- Include one or more JavaScript File(s).  E.g. 'myjsfunctions.js' or ['myjsfunctions1.js','myjsfunctions2.js']
kev1n
nuBuilder Team
 
Posts: 1763
Joined: Mon Oct 15, 2018 2:13 am
nuBuilder Version: 4.5

Re: Including Javascript

Unread postby icoso » Thu May 20, 2021 2:11 am

That wont work. The javascript is only pertinent to the fields on this one form. I don't want or need it loaded globally. The only way taht would work is if I put all the code in a function and then call the function from the Custom Code.

Also, Im now having issues with using Hash Cookies since I lost my form.

Im trying to set a global hash cookie in my forms custom code and then use that global hash cookie in the custom code on a field on that same form. Im getting and undefined message.

For example: on my forms custom code:

Code: Select all
var formsql = 'SQL ';
nuSetProperty('FRM_search', formsql, true);


If then do a javascript alert(formsql); in the same custom code right after I set the property it displays the SQL text. If I do this: alert($('#FRM_search').val()); I get undefined. WHY?

Then on one of my fields on that form in the custom code on that field I do this:

Code: Select all
var sql = $('#FRM_search').val();
alert("SQL="+ sql);


I get an undefined for the sql variable. WHY?
icoso
 
Posts: 173
Joined: Mon Feb 08, 2021 7:39 am
nuBuilder Version: 4.5

Re: Including Javascript

Unread postby kev1n » Thu May 20, 2021 2:22 am

To set: nuSetProperty()

To get: nuGetProperty()

$('#FRM_search') gets a reference to a DOM element, not a Hash Cookie.

But iirc, nuGetProperty() won't work for global hash cookies (client-side retrieval), just on server side.
Can you use nuSetProperty() without setting them to global scope? Or maybe window.yourvariable = 'something' or sessionStorage / sessionStorage to set/get variables?
kev1n
nuBuilder Team
 
Posts: 1763
Joined: Mon Oct 15, 2018 2:13 am
nuBuilder Version: 4.5

Re: Including Javascript

Unread postby kev1n » Thu May 20, 2021 2:34 am

icoso wrote:That wont work. The javascript is only pertinent to the fields on this one form. I don't want or need it loaded globally. The only way taht would work is if I put all the code in a function and then call the function from the Custom Code.


I'm sure this will work. Functions are only run when they are executed.
Otherwise, try using https://api.jquery.com/jquery.getscript/
kev1n
nuBuilder Team
 
Posts: 1763
Joined: Mon Oct 15, 2018 2:13 am
nuBuilder Version: 4.5


Return to Custom Code

Who is online

Users browsing this forum: No registered users and 10 guests