This is the structure of my HTML:
<html>
<body>
<head>
...
<script type='text/javascript' src="js/javascript.js"></script>
</head>
...
<button class="submit" onclick="submitted()">Submit</button>
</body>
</html>
My file directory is Desktop/Project/. Within the project folder the HTML file is on its own and there is a subfolder called js where javascript.js exists.
Within the external js file it goes something like this:
function submitted(){
...
subtask(variable)
...
}
function subtask(param){
...
}
Within submitted() I added alert('hello'); as the first thing and it wouldn't trigger the alert when I refresh the html page. Am I doing something wrong?
-
Are any errors being reported? Check the "Console" in your browser's dev tools.Jonathan Lonowski– Jonathan Lonowski2016年04月25日 03:49:43 +00:00Commented Apr 25, 2016 at 3:49
-
@timolawl it's a typo while I was posting this question here. The typo does not exist in my actual project filebtrballin– btrballin2016年04月25日 03:50:39 +00:00Commented Apr 25, 2016 at 3:50
-
1Why do you expect the alert when you refresh the page? You call the function when the button is clicked, not on page load. Please state the problem more clearly.nnnnnn– nnnnnn2016年04月25日 03:52:32 +00:00Commented Apr 25, 2016 at 3:52
-
Check the console as @JonathanLonowski said.QoP– QoP2016年04月25日 03:53:10 +00:00Commented Apr 25, 2016 at 3:53
-
Example: jsfiddle.net/mow6482tuser2884707bond– user2884707bond2016年04月25日 03:53:39 +00:00Commented Apr 25, 2016 at 3:53
2 Answers 2
Change your <script> tag to:
<script type="text/javascript" src="js/javascript.js"></script>
.. means "one directory above the current directory".
5 Comments
submit(){ alert("hi"); } in the js file and change the submit button HTML to <button class="submit" onclick="submit()">Submit</button> but it won't trigger the alert...submit() function. When I check the console, the source code doesn't even have the newly created submit function even though I saved the source code and refreshed the HTML page.According to your descriptions, the html file is within project folder and the js file is within project/js folder, then the src path ../js/javascript.js must be wrong. Try js/javascript.js
Originally, I thought it was because you put your script tag within head, not after button tag. But I tried it. It works well with script in head