0

I am encountering the following error in my application:

error that next.js returns to me

I imported my js file in _app.js and I start to get this error, I would like to know how I can use my script in my application the script code is as follows:

    function toggleDD(myDropMenu) {
        document.getElementById(myDropMenu).classList.toggle("invisible")
    }
    
     function filterDD(myDropMenu, myDropMenuSearch) {
        var input, filter, ul, li, a, i;
        input = document.getElementById(myDropMenuSearch);
        filter = input.value.toUpperCase();
        div = document.getElementById(myDropMenu);
        a = div.getElementsByTagName("a");
        for (i = 0; i < a.length; i++) {
            if (a[i].innerHTML.toUpperCase().indexOf(filter) > -1) {
                a[i].style.display = "";
            } else {
                a[i].style.display = "none";
            }
        }
    }
window.onclick = function(event) {
    if (!event.target.matches('.drop-button') && !event.target.matches('.drop-search')) {
        var dropdowns = document.getElementsByClassName("dropdownlist");
        for (var i = 0; i < dropdowns.length; i++) {
            var openDropdown = dropdowns[i];
            if (!openDropdown.classList.contains('invisible')) {
                openDropdown.classList.add('invisible');
            }
        }
    }
}

and imported it that way:

 import "tailwindcss/tailwind.css";
    import '../public/wrapper'
    
    import Head from "next/head";
    
    function MyApp({ Component, pageProps }) {
      return (
        <>
          <Head>
            <meta charset="UTF-8" />
            <meta name="viewport" content="width=device-width, initial-scale=1.0" />
            <meta http-equiv="X-UA-Compatible" content="ie=edge" />
            <title>Tailwind Admin Starter Template : Tailwind Toolbox</title>
            <meta name="author" content="name" />
            <meta name="description" content="description here" />
            <meta name="keywords" content="keywords,here" />
    
            <link
              rel="stylesheet"
              href="https://use.fontawesome.com/releases/v5.3.1/css/all.css"
            />
            <link
              href="https://unpkg.com/tailwindcss/dist/tailwind.min.css"
              rel="stylesheet"
            />
            <link
              href="https://afeld.github.io/emoji-css/emoji.css"
              rel="stylesheet"
            />
            <script
              src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.8.0/Chart.bundle.min.js"
              integrity="sha256-xKeoJ50pzbUGkpQxDYHD7o7hxe0LaOGeguUidbq6vis="
              crossorigin="anonymous"
            ></script>
             <link href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:400,700" rel="stylesheet" />
            
          </Head>
    
          <Component {...pageProps} />
          <script src="/wrapper.js"></script>
        </>
      );
    }

export default MyApp;

I tried several alternatives that I found out there and I could not solve this something is going unnoticed if someone can help me thank you

2
  • Why are you doing things like ` document.getElementById` is a React application? Commented Apr 29, 2021 at 17:12
  • in fact this is an html template that I'm trying to pass to Next.js and I wanted to change it as little as possible, but I believe that I will have to change that Commented Apr 29, 2021 at 17:41

1 Answer 1

1

When you load your script wrapper.js server-side, it gets executed, and there is no window object on the server, hence the error.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.