簡體   English   中英

無法從body onload調用函數(未捕獲的參考錯誤:未定義resetLoginForm)

[英]Can't call function from body onload (uncaught reference error: resetLoginForm is not defined) javascript

我有一個身體onload在javascript中調用函數。 我已經嘗試了很多事情,但是控制台只會打印到錯誤日志:

未捕獲的參考錯誤:未定義resetLoginForm

我的代碼如下:

<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/html">

    <head>

        <title>Login Page</title>

        <script src="/client/js/popper.min.js"></script>
        <script src="/client/js/js.cookie.min.js"></script>
        <script src="/client/js/querystring.js"></script>
        <!--Import Google Icon Font-->
        <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
        <!--Import materialize.css-->
        <link type="text/css" rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css"  media="screen,projection"/>
        <!--Let browser know website is optimized for mobile-->
        <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
        <script type="module" src="js/login.js"></script>

    </head>

    <body class="blue-grey darken-2" onload="resetLoginForm()">

        <script src="js/jquery-3.3.1.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"></script>

這是我嘗試使用的JavaScript代碼:

import * as Cookies from "/client/js/js.cookie.min.js";

function resetLoginForm() {

    if (Cookies.get("destination") === undefined) {
        window.location.href = "/client/index.html";
    }

    const loginForm = $('#loginForm');
    loginForm.submit(event => {
        event.preventDefault();
        $.ajax({
            url: '/admin/login',
            type: 'POST',
            data: loginForm.serialize(),
            success: response => {
                if (response.startsWith('Error:')) {
                    alert(response);
                } else {
                    Cookies.set("sessionToken", response);
                    window.location.href = Cookies.get("destination");
                }
            }
        });
    });

您正在使用新的ECMAScript模塊( <script type="module" ...> ),它們獨立於它們自己的范圍。 您的函數resetLoginForm()不在全局范圍內定義(實際上您可以從onload主體函數調用它)。 您需要在模塊中明確定義它:

import * as Cookies from "/client/js/js.cookie.min.js";

window.resetLoginForm = {

    if (Cookies.get("destination") === undefined) {
        window.location.href = "/client/index.html";
    }
    ...

理想情況下 ,您根本不應該使用onload 只需在以下位置添加事件偵聽器:

import * as Cookies from "/client/js/js.cookie.min.js";

function resetLoginForm() {

    if (Cookies.get("destination") === undefined) {
        window.location.href = "/client/index.html";
    }

    ...
}

// Attach an event, and call resetLoginForm when the document is done loading.
document.addEventListener("DOMContentLoaded", resetLoginForm);
  1. 更改為<script type="text/javascript" src="js/login.js"></script>
  2. 請在onload中檢查一次resetLoginForm函數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM