Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
543 views
in Technique[技术] by (71.8m points)

html - Can't call function from body onload (uncaught reference error: resetLoginForm is not defined) javascript

I have a body onload calling a function in javascript. I Have tried many things, but the console just prints to the error log:

uncaught reference error: resetLoginForm is not defined

My code is as follows:

<!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>

This is the JavaScript code that I am trying to use:

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");
                }
            }
        });
    });
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You're using new ECMAScript modules (<script type="module" ...>), which are isolated to their own scope. Your function resetLoginForm() isn't being defined in the global scope (Where you can essentially call it from your onload body function). You need to define it explicitly in your module:

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

window.resetLoginForm = {

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

Ideally though, you should not be using onload at all. Just add an event listener in:

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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...