R
R
Roman2019-08-13 12:32:54
JavaScript
Roman, 2019-08-13 12:32:54

JS doesn't see variables and functions from included scripts?

Hello.
Faced the following problem. I write a function in a js file, connect this file to the desired page, and when I call the function there, in the console, I get an error: Uncaught ReferenceError: hello is not defined
JS:

function hello(name) {
    console.log('Hello', name);
}
let selectedUser = {
    dismiss: 0,
    sub: 0
};

function dismiss(result) {
    selectedUser.dismiss = result.id;
}

function sub(result) {
    selectedUser.sub = result.id;
}

function transferData() {
    if(document.getElementById('taskExists').checked === true) {
        selectedUser.tasks = {taskExists: true};
    }
    if(document.getElementById('taskExpired').checked === true) {
        selectedUser['tasks']['taskExpired'] = true;
    }
    return selectedUser;
}

HTML:
<!doctype html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <link rel="stylesheet" href="css/style.css">
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
        <script type="text/javascript" src="https://code.jquery.com/jquery-1.10.0.min.js"></script>
        <script src="https://ajax.googleapis.com/ajax/libs/d3js/5.9.0/d3.min.js"></script>
        <script src="//api.bitrix24.com/api/v1/"></script>
        <script src="https://kit.fontawesome.com/895032afd0.js"></script>                
    </head>
    <body>
        <script>hello('Roman');</script>
        <button id="dismissUser" class="btn btn-md btn-danger mt-1 mb-1" onclick="BX24.selectUser(dismiss);"><i class="fas fa-user-times icon"></i> Увольняющийся сотрудник</button><br />
        <button id="subUser" class="btn btn-md btn-warning mt-1 mb-1" onclick="BX24.selectUser(sub)"><i class="fas fa-user-check icon"></i> Замещающий сотрудник</button>
        <p><strong>Передаваемые дела</strong></p>
        <details>
            <summary>Задачи</summary>
            <input onchange="transferData()" id="taskExists" type="checkbox"> Передать задачи<br />
            <input onchange="transferData()" id="taskExpired" type="checkbox"> Передать просроченные и почти просроченные задачи<br />
        </details>
        <p><button id="sendData" class="btn btn-md btn-success">Отправить</button></p>
        <script async src="script.js"></script>
    </body>
</html>

And when I wrote JS on the same page earlier, everything worked fine (although the declaration of variables and functions was after they were called). And then I had to take out the code in a separate file - off we go.
5d5284378a0b2748884409.png

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Karvasarny, 2019-08-13
@megapihar6

but nothing that you have a file with a function connected below the call?) you call it and it has not yet been initialized.

R
Roman, 2019-08-13
@Ramapriya

First <script async src="script.js"></script>was in head, the result is the same

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question