2

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() ....
4
  • 2
    Does your docket.js files actually contain the opening and closing <script> tags? It shouldn't Commented Oct 11, 2019 at 16:32
  • Also, to help you debugging, you should read the browser console (F12 -> Console), you probably have an error showing up there. Commented Oct 11, 2019 at 16:34
  • It did, I've tried it both ways. Removed them. - still doesn't work. Commented Oct 11, 2019 at 16:35
  • try adding window.addEventListener('load', doFunction); in your js file and remove onload="dofunction()" from your html Commented Oct 11, 2019 at 16:44

2 Answers 2

1

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

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

4 Comments

OP claims they tried it both ways and it didn't make a difference
Yes - removed them in the file (and above too)
See below - my bad. Thanks, though. The no scripts thing helped.
@thf No problem, it is also good practice to put the script at the bottom before the closing body tag.
0

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>

Comments

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.