There are several problems with your source that stopped Javascript from running: Show
You were missing the closing quotes for the src attribute. That made the DOM interpret everything after that as part of the src attribute, screwing up everything.
You had a closing curly bracket instead of an opening one. This caused a parse error because Javascript was expecting an opening curly bracket.
You had an invisible character before the 1. This one was particularly difficult to find—I had to use my Javascript debugger to find it.
This shouldn't affect your problem, but you were missing the equals sign between the Since you seem to be having trouble getting it to work, copy and paste the below and see if it works. (For me, without editing the textboxes, if you click the submit button, you get an alert right away). If it doesn't work, let me know what browser you're using.
This may happen if you haven’t used “export” statement. Use “export” before the function which will be imported into the script file. The JavaScript file is as follows which has the file name demo.js. demo.js console.log("function will import"); export function test(){ console.log("Imported!!!"); } Here is the “index.html” file that imports the above function − index.html ExampleLive Demo <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initialscale=1.0"> <title>Document</title> <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"> <script src="https://code.jquery.com/jquery-1.12.4.js"></script> <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> </head> <body> <script type='module'> import { test } from "./demo.js" test(); </script> </body> </html> To run the above program, save the file name “anyName.html(index.html)” and right click on the file. Select the option “Open with Live Server” in VS Code editor. Following is the output from the file demo.js which has the function name test().
Updated on 02-Sep-2020 06:14:29
Why is my JavaScript not working in HTML?On the web browser menu click on the "Edit" and select "Preferences". In the "Preferences" window select the "Security" tab. In the "Security" tab section "Web content" mark the "Enable JavaScript" checkbox. Click on the "Reload the current page" button of the web browser to refresh the page.
How do I run a JavaScript function from HTML?The first method is to call the JavaScript function in HTML. For this, you have to create a function then define this function either in the head section or body section of the HTML document. You can either create a link or a button and then an onclick() event is associated with them in order to call this function.
Why is JavaScript saying my function is not defined?You're Calling the Function Before It's Defined
If the code that calls your JavaScript function precedes that function's definition in your HTML document, you will come across the function is not defined error. This is very, very important to check and rule out before you proceed to investigating other probable causes.
Where do I put JavaScript function in HTML?Incorporating JavaScript in the HTML Header Attribute
js file. You can add JavaScript functions to a page by entering the code in the HTML Header attribute on the Page Attributes page.
|