简体   繁体   English

外部脚本中的 javascript 函数

[英]javascript functions in external scripts

I'm learning javascript for a project, but I am stuck at the very beginning.我正在为一个项目学习 javascript,但我一开始就被困住了。 I boiled it down, to the function in my script not being defined, but as near as I can tell it is defined.我把它归结为我的脚本中的 function 没有被定义,但据我所知它已被定义。

I have a script: "script.js" with the function display result.我有一个脚本:“script.js”,显示结果为 function。

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

in the header of index.html I have this line在索引的 header.html 我有这条线

<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.我不知道为什么我的 function 不会打电话。 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.脚本块没有href属性,它通过src属性从外部源包含。

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).顺便说一句,在文档加载完成后调用document.write将首先清除文档的全部内容,然后将其替换为您传递给调用的任何内容(在这种情况下,'hello world',它不是有效的 HTML 或 XML文档)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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