I am tearing my hair out over this. Why is foo() undefined when I click the button in this script?
JavaScript
x
13
13
1
<html>
2
<body>
3
<script type="text/javascript" src="./app2.js"/>
4
<script">
5
function foo() {
6
console.log('foo...');
7
}
8
</script>
9
<button type="button" onClick="foo()" id="testbutton">Click!</button>
10
<button type="button" onClick="hello()">Click hello!</button>
11
</body>
12
</html>
13
but not if I remove the first script tag?
JavaScript
1
12
12
1
<html>
2
<body>
3
<!-- <script type="text/javascript" src="./app2.js"/>-->
4
<script>
5
function foo() {
6
console.log('foo...');
7
}
8
</script>
9
<button type="button" onClick="foo()" id="testbutton">Click!</button>
10
</body>
11
</html>
12
My app2.js is just
JavaScript
1
4
1
function hello() {
2
console.log('hello');
3
}
4
I have tested in Chrome and Safari on macOS. The hello function works as expected.
Advertisement
Answer
Auto closing tags are used in React JSX and not in vanilla HTML Replace
JavaScript
1
2
1
<script type="text/javascript" src="./app2.js"/>
2
with
JavaScript
1
2
1
<script type="text/javascript" src="./app2.js" ></script>
2