Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
52
rated 0 times [  54] [ 2]  / answers: 1 / hits: 21029  / 11 Years ago, thu, may 16, 2013, 12:00:00

I can't figure out what's wrong here and I'm not getting any output from the Javascript file. I am trying to use src to be able to type my javascript file outside of the index file. This just prints the header from the html file A test heading without printing the text variable. If I type the code within the html file it works fine. Both scripts are in the same folder.



<!DOCTYPE HTML>

<html>
<head>
<h2>
A Test Heading
</h2>
<script language = JavaScript src=/slider.js></script>
</head>
</html>


Here's the file labeled slider.js -



function slider(){  
var text = Welcome to Slider Simulator 2013!;

document.write(text);
}
slider();

More From » html

 Answers
20

If the script is in the same folder as the HTML file, then you can just do:



<script src=slider.js></script>


The starting / means from the root, and the root might not be what you'd expect. Root does not mean the location of the HTML that loaded it, but the root of the file system or the domain.



Content must be placed inside the <body>, and not anywhere else. <head> is usually for scripts, styles, page metadata, but not the content.



Also, language can be omitted since <script> run JavaScript by default anyway.


[#78174] Thursday, May 16, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
charisma

Total Points: 1
Total Questions: 99
Total Answers: 117

Location: Thailand
Member since Thu, Apr 22, 2021
3 Years ago
;