Skip to content Skip to sidebar Skip to footer

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 defi

Solution 1:

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 * asCookiesfrom"/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 * asCookiesfrom"/client/js/js.cookie.min.js";

functionresetLoginForm() {

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

Solution 2:

  1. Change is <script type="text/javascript" src="js/login.js"></script>
  2. Cnages is please check once resetLoginForm function in onload.

Post a Comment for "Can't Call Function From Body Onload (uncaught Reference Error: Resetloginform Is Not Defined) Javascript"