thf
thf

Reputation: 604

External javascript not working - local file

I am using a local HTML file with link to an external script (located in the same directory) that worked when in the file, but externally doesn't. Neither Firefox nor Chrome. Code:

<html>
<head>
<script type="text/javascript" src="docket.js"> </script>
</head>
<body onload="doFunction()"> ...

The script is in a .js file, which has (simplified):

function doFunction() ....

Upvotes: 2

Views: 3010

Answers (2)

pritam
pritam

Reputation: 2558

As per w3school - https://www.w3schools.com/tags/tag_script.asp

The external script file cannot contain the tag.

The syntax for script tag is -

<script src="URL">

Where your URL is - 1. An absolute URL - points to another web site (like src="http://www.example.com/example.js") OR 2. A relative URL - points to a file within a web site (like src="/scripts/example.js")

Note: with HTML5, type attribute for script tag is optional.

I'd suggest to add a noscript tag just to make sure you have JS enabled in your browser.

<noscript>Your browser does not support JavaScript!</noscript>

Upvotes: 0

corix010
corix010

Reputation: 571

For one, you shouldn't include the script tags in your external js file.

Also try to move the script line at the bottom before the closing body tag.

If after removing, it still doesn't work, you should open the developer tools to get clue of what is going on.

index.html:

<!DOCTYPE html>
<head>
    ...
</head>
<body onload="doFunction()">
    ...
    <script type="text/javascript" src="docket.js"></script>
</body>
</html>

docket.js:

function doFunction() {
    alert("hello...");
}

Note: no script tags

Upvotes: 1

Related Questions