jactor-rises
jactor-rises

Reputation: 3091

How to invoke a function in javascript which has been declared in a html file

I have a html file with function displayJsonWithAjax and function displayOtherJsonWithAjax declared in a script-tag.

In another script-tag, I invoke those functions with this code when a select box change:

<script>
import fetchJson from 'some.module'

function displayJsonWithAjax() {
     ...
}

function displayOtherJsonWithAjax() {
     ...
}

</script>

<script>
$(document).ready(function () {
    $('#selectBox').change(function () {
        displayJsonWithAjax();
        displayOtherJsonWithAjax();
    }).change();
});
</script>

When debugging with a browser, I get the following error:

ReferenceError: displayJsonWithAjax is not defined

If I try to put all the functions in the same script tag, no code is automatically executed when the browser load the page... How do I accomplish to call these two functions?

Upvotes: 0

Views: 59

Answers (2)

LellisMoon
LellisMoon

Reputation: 5020

test();
<script>function test(){
alert('hello');
}</script>

Upvotes: 0

Xenos
Xenos

Reputation: 3507

import fetchJson from 'some.module'

Does that really work? Check you console.

If a script line fails, everything after that line won't be executed, so the script functions won't be declared and won't be usable elsewhere (and that will explain why "If I try to put all the functions in the same script tag, no code is automatically executed when the browser load the page": the script fails at line 1, and nothing else is executed).

Upvotes: 3

Related Questions