简体   繁体   中英

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. I know this is probably a simple question, but I have been searching around for about an hour with no luck.

Thanks Rusty

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM