-2

I'm dealing with serious issue here. My JavaScript file doesn't seem to be linking to my HTML file. I have them all in the same location, so that's not the problem. My script link is also fine in HTML. It just doesn't seem to link strange enough. The JavaScript statements seem to work when I use script tags in my HTML-file, but the same statements just won't load when I use them in a separate JavaScript file.

This seems to be fine:

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

I'm using Brackets by the way.


I tried using the full path to the JS-file and that didn't work either. I don't have any webservers. It's an assignment for school where I need to construct a form for patients. I designed it with CSS and now I'm trying to bring some logic into it using JS, but my JS-file doesn't want to link it. Maybe it did link, but it doesn't want to work somehow. This is the code Im trying to run in the JS-file:

var elem = document.getElementById("patientnummer"); elem.value = "12345678";

No errors at all. It works fine when I use this exact code in script-tags in my HTML-file, but it doesnt work when I use it in a JS-file.

Martijn Pieters
  • 889,049
  • 245
  • 3,507
  • 2,997
Toufik
  • 93
  • 1
  • 7
  • Brackets doesn't give any errors as well, so I don't really understand why I doesn't wanna link. – Toufik Sep 20 '15 at 15:51
  • 1
    ‘*This seems to be fine*’ OK then. What's the problem? – Biffen Sep 20 '15 at 16:00
  • Well, the statement doesn't do what it should do. I set a default value for an input-field. This worked fine when I used script-tags in the HTML-file, but it doesn't work when I use the same statements in the JS-file even though..Brackets claims there are no errors. – Toufik Sep 20 '15 at 16:03
  • 1
    show the actual code ... are you using a load event handler to make sure elements exist when code runs? – charlietfl Sep 20 '15 at 16:03

2 Answers2

-2

Use global statement, like this:

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

All links in HTML files must be start with /.

Anders
  • 7,431
  • 6
  • 42
  • 76
Illia Moroz
  • 303
  • 2
  • 12
  • 1
    ‘*All links in html files must be started from '/'*’ [citation needed] – Biffen Sep 20 '15 at 15:55
  • 1
    OP stated the files are all in same directory and paths can also be relative so last statement is not correct ... and neither will that path work – charlietfl Sep 20 '15 at 15:55
  • Exactly, they are in the same directory. So this doesn't do much for me. – Toufik Sep 20 '15 at 15:57
  • If you have .htaccess file even you have all scripts in same directory, if you insert script on this link http://example.com/ it will get the script from /script.js But if you have link for example like this http://example.com/news/ (htaccess replacement) your script will be linked on http://example.com/news/script.js That because gobal linking - it is the base thing you must to use. – Illia Moroz Sep 20 '15 at 16:04
  • 2
    but that doesn't mean relative url's can't be used, or that a `` tag can't be put in page. There are alternatives – charlietfl Sep 20 '15 at 16:07
  • It's strange, because my CSS-file is in the same directory and it doesn't have any problemens with linking to my HTML-file. Than why does it have problemens with my JS-file? – Toufik Sep 20 '15 at 16:12
-2

If you are serving your HTML file via the file:// protocol, then you will need to use the full path to your JavaScript file.

If you are serving your HTML file via a web server (http:// or https://), then it could depend on your web server, perhaps it doesn't like certain file names. Without more information on your environment, all I can do is speculate.

My suggestions would be to try all of the following:

  1. Use full path to the file.

  2. Change file from script.js to myStuff.js and see if that works.

  3. Try ./script.js and /script.js instead of simply script.js in the script tag.

Biffen
  • 5,354
  • 5
  • 27
  • 32