• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

175
Views
Why does my simple script tag break the following script tag?

I am tearing my hair out over this. Why is foo() undefined when I click the button in this script?

<html>
    <body>
        <script type="text/javascript" src="./app2.js"/>
        <script">
            function foo() {
                console.log('foo...');
            }
        </script>
        <button type="button" onClick="foo()" id="testbutton">Click!</button>
        <button type="button" onClick="hello()">Click hello!</button>
    </body>
</html>

but not if I remove the first script tag?

<html>
    <body>
<!--        <script type="text/javascript" src="./app2.js"/>-->
        <script>
            function foo() {
                console.log('foo...');
            }
        </script>
        <button type="button" onClick="foo()" id="testbutton">Click!</button>
    </body>
</html>

My app2.js is just

function hello() {
    console.log('hello');
}

I have tested in Chrome and Safari on macOS. The hello function works as expected.

almost 3 years ago · Santiago Gelvez
1 answers
Answer question

0

Auto closing tags are used in React JSX and not in vanilla HTML Replace

<script type="text/javascript" src="./app2.js"/>

with

<script type="text/javascript" src="./app2.js" ></script>
almost 3 years ago · Santiago Gelvez Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error