Rusty
Rusty

Reputation: 389

JavaScript functions in external scripts

I'm learning JavaScript for a project, but I am stuck at the very beginning. I boiled it down, to the function in my script not being defined, but as near as I can tell it is defined.

I have a script: "script.js" with the function display result.

function displayResult()
{     
    document.write("hello world");
}

in the header of index.html I have this line

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

I have this line later

<body onload="displayResult()">

I have no idea why my function will not call. I would appreciate the help.

Upvotes: 0

Views: 78

Answers (2)

RobG
RobG

Reputation: 147343

BTW, calling document.write after the document has finished loading will first clear the entire content of the document, then replace it with whatever you pass to the call (in this case, 'hello world', which is not a valid HTML or XML document).

Upvotes: 0

Chad
Chad

Reputation: 19609

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

Should be:

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

there is no href attribute to a script block, its included from an external source through the src attribute.

Upvotes: 2

Related Questions