Call External JavaScript Functions from Java Code

Call external javascript functions from java code

Use ScriptEngine.eval(java.io.Reader) to read the script

ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("JavaScript");
// read script file
engine.eval(Files.newBufferedReader(Paths.get("C:/Scripts/Jsfunctions.js"), StandardCharsets.UTF_8));

Invocable inv = (Invocable) engine;
// call function from script file
inv.invokeFunction("yourFunction", "param");

Call js methods from java

The available answers provide enough information to call javascript code using Nashorn, specially the duplicated one that suggests 31piy in the first comment.

Then I assume that your real problem is to reach the javascript file that contains your code, present in your webapp folder. I can see some possible answers to that:

Here some fellows suggest possible ways to reach the file using Spring methods: Accessing files/directories in webapp folder in Spring You should consider if there is a way available to your particular framework

However: is there a good reason for that file to be in your webapp folder? Is its logic necessary at all in the client side? Since you need the logic in the server side, I would not (even for prudence) expose it to the client of your application. If it is not necessary in the client side, don't expose it. Just keep it, let's say, in your application's src/main/resources directory (assuming you build it with maven or gradle) and access it cleanly from your classloader:

// Access the resource from the classloader
try {
reader = new InputStreamReader(
YourServletOrWhateverClass.class.getClassLoader().
getResourceAsStream("your_javascript_file.js"),
Charset.forName("UTF-8"));
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("JavaScript");
engine.eval(reader);

Invocable inv = (Invocable) engine;
inv.invokeFunction("your_function");
}
catch(NoSuchMethodException nsme) {
nsme.printStackTrace();
}
catch(ScriptException se) {
se.printStackTrace();
}
finally {
try {
reader.close();
}
catch(IOException ioe) {
ioe.printStackTrace();
}
}

How to call external JavaScript function in HTML

If a <script> has a src then the text content of the element will be not be executed as JS (although it will appear in the DOM).

You need to use multiple script elements.

  1. a <script> to load the external script
  2. a <script> to hold your inline code (with the call to the function in the external script)

    scroll_messages();

External Javascript calling functions from another external javascript file

I come from the same environment that you came. I did the javascript course that helped me a lot, codeacademy.com.

Well I should implement your sample like this:

fighter.js

function Fighter(){
this.health = 100;
this.energy = 100;

this.isDead = function() {

if (health < 10 || energy < 10)
{
return dead = 'true';
}
else {
return dead;
}
}

this.Punch = function(otherFighter){
otherFighter.energy -= 10;
otherFighter.health -= 10;

}

this.AtackWithItem(item, fighter){
otherFighter.energy -= item.damage;
otherFighter.health -= item.damage;
}
}

main.js

function initFight() {

var fighterA = new Fighter();
var fighterB = new Fighter();

document.getElementById("energy").innerHTML = energy;
document.getElementById("health").innerHTML = health;
document.getElementById("sword").innerHTML=sword;

fighterB.punch(fighterA);
document.getElementById("energy").innerHTML = energy;
document.getElementById("health").innerHTML = health;
document.getElementById("sword").innerHTML=sword;

var sword = new item();
fighterA.AtackWithItem(sword,fighterB);
document.getElementById("energy").innerHTML = energy;
document.getElementById("health").innerHTML = health;
document.getElementById("sword").innerHTML=sword;

}

item.js

function item(){
this.type = "Swords"
this.damage = 20;
}

All files fighter.js, main.js and item.js will be refer with the tag:

<script src="../item.js"></script> 
<script src="../fighter.js"></script>
<script src="../main.js"></script>

in the index.html.

Hope this help you...

How to call external javascript function in angular 5?

You can use javascript in the Angular application.

Step 1. Create demo.js file in assets/javascript folder.

export function test1(){
console.log('Calling test 1 function');
}

Step 2. Create demo.d.ts file in assets/javascript folder.

export declare function test1();

Step 3. Use it in your component

import { test1 } from '../assets/javascript/demo'; 
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
constructor() {
console.log(test1());
}
}

Note: js and .d.ts file name should be same



Related Topics



Leave a reply



Submit