Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

External JavaScript Not Running, does not write to document

I'm trying to use an external JavaScript file in order to write "Hello World" into a HTML page.

However for some reason it does not work, I tried the same function and commands inline and it worked, but not when it's using an external JavaScript file. The part I commented out in the JS file was the previous method I was trying to use. Those lines of could worked when I ran the script from the header, and inline. Thanks

Html file:

<html>

    <head>
    </head>

<body>

    <p id="external">

        <script type="text/javascript" src="hello.js">
            externalFunction();
        </script>
    </p>

    <script type="txt/javascript" src="hello.js"></script>

</body>

</html>

JavaScript file

function externalFunction() 
        {
         var t2 = document.getElementById("external");

            t2.innerHTML = "Hello World!!!"

         /*document.getElementById("external").innerHTML = 
         "Hello World!!!";*/

        }
like image 485
Jon Q Avatar asked Nov 08 '14 20:11

Jon Q


People also ask

How do I write code in an external JavaScript file?

To include an external JavaScript file, we can use the script tag with the attribute src . You've already used the src attribute when using images. The value for the src attribute should be the path to your JavaScript file. This script tag should be included between the <head> tags in your HTML document.

Is it necessary to write script tag in case of external js file?

The use of external JavaScript is more practical when the same code is to be used in many different web pages. Using an external script is easy , just put the name of the script file(our . js file) in the src (source) attribute of <script> tag. External JavaScript file can not contain <script> tags.

How do I enable JavaScript on my HTML file?

You can add JavaScript code in an HTML document by employing the dedicated HTML tag <script> that wraps around JavaScript code. The <script> tag can be placed in the <head> section of your HTML or in the <body> section, depending on when you want the JavaScript to load.

What file type is given to external JavaScript files?

An external JavaScript file must be saved by . js extension.


2 Answers

In general, you want to place your JavaScript at the bottom of the page because it will normally reduce the display time of your page. You can find libraries imported in the header sometimes, but either way you need to declare your functions before you use them.

http://www.w3schools.com/js/js_whereto.asp

index.html

<!DOCTYPE html>
<html>

<head>
  <!-- You could put this here and it would still work -->
  <!-- But it is good practice to put it at the bottom -->
  <!--<script src="hello.js"></script>-->
</head>

<body>

  <p id="external">Hi</p>

  <!-- This first -->
  <script src="hello.js"></script>

  <!-- Then you can call it -->
  <script type="text/javascript">
    externalFunction();
  </script>

</body>

</html>

hello.js

function externalFunction() {
  document.getElementById("external").innerHTML = "Hello World!!!";
}

Plunker here.

Hope this helps.

like image 120
Pedro Lopez Avatar answered Sep 28 '22 16:09

Pedro Lopez


Script tags with SRC values do not run the contents. Split it to two script tags. One for the include, one for the function call. And make sure the include is before the call.

like image 36
rlemon Avatar answered Sep 28 '22 16:09

rlemon