How to call a function defined in a JavaScript-file?

I added a small javascript file to the folder “JavaScripts”.

The file contains one simple function definition.

In a script that is located in the “Scripts”-panel, I added a new script and tried to call the function defined in the file.

In the script-editor I always get the errormessage, that the function is unknown.

The Script-file is check-marked in “Section/Includes…”.

Is something missing?

I’d like to have an example that uses a JS-file to define a simple function and the corresponding script that calls the function.

2 Likes

Hello Thomas,

As I said in previous responses, JavaScript and Scripts are not the same thing - they do not interact in any way, shape or form.

However, you’re not completely out of luck. You can’t “include” a Script inside another, however you can define variables and functions globally, so they can be accessed in other scripts processed after it.

For example, very simple hello world function placed inside a first script with the body selector:

hello_world = function(name) { // Note the missing var keyword! This makes it global…
return "Hello, " + name;
};

Then, a second script on a paragraph, say p.hellomessage:

results.html(hello_world(“mike”));

This diplays “Hello, mike” in the paragraph with the class “hellomessage”. Both variables and functions can be declared globally like this, so if you create a Script with your function definitions and contants, you’ll be fine. However, there’s currenly no way to export and import scripts so you’ll have to use copy/paste…

1 Like

That sounds good - but as we are just starting to understand what can be done where and how - could you provide a demo “OL-template” - file that demonstrates that?
That would be really great!
Thanks for your quick answers.

Huh, seems like our new Q&A doesn’t let us attach files. We’ll have to look into that. In the meantime, yay for being a web administrator!

http://help.objectiflune.com/files/connect-samples/Sample-Using-Global-Variables-And-Functions.OL-template

Thanks a lot - that shows how to do it.

I downloaded http://help.objectiflune.com/files/connect-samples/Sample-Using-Global-Variables-And-Functions.OL-template but it appears to be empty when I open it in designer.

Hello Rodger,

The sample is very simple and doesn’t contain any design or static elements. Go to the Preview tab to see the result of the script, which prints “Hello, Mike, and welcome to Globadyne, Inc.”

Both the hello_world() function and the COMPANY_NAME variable are defined in the first script, and used in the second script, demonstrating that they are, indeed, global.

Hi All

I have downloaded this file and I appear to get the wrong Connect Files.

demo-cotg-showoutput.OL-datamodel

Can you check it out please.
Thanks

John

Hello John,

You must be looking at something else - there is no such file on the demo server, at least not in the public files. Are you sure you didn’t download it to a different location? It works fine for me…

~Evie

Hi Evie

There must have been a chaching issue with downloads on Windows 10 as I tried a my laptop and it downloaded fine.

Thanks for checking it out.

Kind Regards

John

good how do I call the javascript folder from the html?

Is this for a print content or client side in a web content?

In case of print content I suggest to add the function to a Control Script of the Scripts panel.