0

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?

6
  • Are any errors being reported? Check the "Console" in your browser's dev tools. Commented 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 file Commented Apr 25, 2016 at 3:50
  • 1
    Why 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. Commented Apr 25, 2016 at 3:52
  • Check the console as @JonathanLonowski said. Commented Apr 25, 2016 at 3:53
  • Example: jsfiddle.net/mow6482t Commented Apr 25, 2016 at 3:53

2 Answers 2

1

Change your <script> tag to:

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

.. means "one directory above the current directory".

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

5 Comments

So I changed the directory link syntax and created a separate function called 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...
That's weird. I'm getting some syntax error even though I commented out all the other code and kept on the 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.
@btrballin, make a JSFiddle and post it please.
Hey Steven. Any idea why my js source code isn't updating? I commented out all my other code and created the simple function with alert, but when I saved and refreshed the HTML page, it doesn't show the js source code being updated with code commented and new simple function added when I check the js console in Chrome.
I'm glad you got this fixed! Could you mark this as answered?
0

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

2 Comments

So when I check the javascript console, it looks like the source code isn't updating. I had 2 functions before. I commented out the 2 functions and added the simple function with alert, but when I see the console, it doesn't show the js code commented out with the new submit function. It just shows the 2 original functions. Why isn't the js code updating when I refresh the page in my browser? I made sure to save the HTML and JS file.
Nvm. I had 2 js files of same name in different locations and I was updating the wrong js file. My issue is resolved!

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.