0

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.

2 Answers 2

2
<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.

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

Comments

0

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).

1 Comment

thanks for the tip. I did not know that. I was using that line just for testing

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.