默认情况下禁用Javascript功能

时间:2020-05-24 11:25:36

标签: javascript import

我创建了一个名为request.js的导出类。 我将此类导入到script.js中,并创建了该对象的新实例。

默认情况下,新实例之后的以下功能将被禁用。我收到了一个Uncaught ReferenceError:未定义LoadLocation。

import Request from './request.js';
let request = new Request();

function LoadLocation() { // << why deactivated by default?

    let LocationTable = document.getElementById('LocationTable');
    LocationTable.innerHTML = '';

    // Method from request.js
    request.execute({
        method: 'GET',
        url: '/location/',
        datatype: 'json',

        successCallback: function (response) {
            if (response) {
                console.log('Get the Location Data');

                for (let location in response) {

                    let tr = document.createElement('tr');
                    let tdName = document.createElement('td');
                    let tdStreet = document.createElement('td');
                    let tdPostcode = document.createElement('td');
                    let tdPlace = document.createElement('td');

                    tr.appendChild(tdName);
                    tr.appendChild(tdStreet);
                    tr.appendChild(tdPostcode);
                    tr.appendChild(tdPlace);

                    tdName.innerHTML = location.location_name;
                    tdStreet.innerHTML = location.street;
                    tdPostcode.innerHTML = location.postcode;
                    tdPlace.innerHTML = location.place;

                    LocationTable.appendChild(tr);
                }
            } else document.getElementById('divMsg').innerHTML = `<div class="alert alert-danger">Daten von Ausgabestellen konnten nicht geladen werden!</div>`;
        },

        errorCallback: function (s, t) {
            console.log('Error: LoadLocation!!!');
            console.error(s + ': ' + t);
        }
    });
}

但是,该功能通过以下方式集成到我的html中:

...
<script>
LoadLocation();
</script>
</body>

我对javascript比较陌生。有人可以给我提示我做错了什么吗?

1 个答案:

答案 0 :(得分:1)

它没有被禁用,只是根本不可见。您在foreach($pos as $po){ $po_total[$po->id] = 0; foreach($po->po_items as $po_item){ $po_total[$po->id]+= $po_item->qty * $po_item->rate; } } 标记中进行的LoadLocation()调用需要/期望在全局script对象上使用函数LoadLocation,但找不到该对象。

快速解决方案:将其添加到脚本中

window

编辑:

当浏览器呈现html时,它将自上而下“执行”。对于脚本,这意味着您自己的脚本(定义{{​​1}}函数并将其分配给window.LoadLocation = LoadLocation; 对象)必须在HTML 上出现,然后在脚本中使用该函数标签。