[英]How to call external javascript on window.load?
I want to put this in my website我想把它放在我的网站上
<script type="text/javascript"> <!-- window.onload = hello; function
hello() { var name = prompt("What is your name", "") alert ( "Hello "
+ name + "! Welcome to my forum.") } </script>
but I dont want to put it in index but in separate file, let say hello.js但我不想把它放在索引中,而是放在单独的文件中,让我们说 hello.js
How can I call it from index file so when I click the index.html, it will immediately prompt for my name.如何从索引文件中调用它,所以当我单击 index.html 时,它会立即提示输入我的名字。 (for example)
(例如)
I put <script src="hello.js"></script>
does not work.我把
<script src="hello.js"></script>
不行。
Your hello.js should look something like this:你的 hello.js 应该是这样的:
window.onload = hello;
function hello() {
var name = prompt("What is your name", "");
alert("Hello " + name + "! Welcome to my forum.");
}
and then the <script src="hello.js"></script>
should work just fine.然后
<script src="hello.js"></script>
应该可以正常工作。
There shouldn't be much more to it than importing that script.除了导入该脚本之外,它不应该有更多的东西。 But then, your script is all broken.
但是,你的脚本全都坏了。 Of course it won't work, unless your browser can interpret it.
当然它不会起作用,除非你的浏览器可以解释它。 Take a look at javascript basics.
看看 javascript 基础知识。 On the first look, you don't seem to be using semicolons to delimit individual statements.
乍一看,您似乎没有使用分号来分隔单个语句。 Also, you have opened an HTML comment
<!--
but you have never closed it...此外,您已经打开了 HTML 评论
<!--
但您从未关闭它...
Change your code to something like this:将您的代码更改为以下内容:
<script type="text/javascript">
var name = prompt("What is your name", "");
alert ( "Hello " + name + "! Welcome to my forum."); </script>
ie, remove any function and just write your alert directly, this should work.即,删除任何 function 并直接编写警报,这应该可以。
Ubuntu Server 16.04 Ubuntu 服务器 16.04
external JS外部JS
change-from: --> window.onload = hello;更改:--> window.onload = 你好;
change-to: --> window.document.body.onload = hello;更改为:-> window.document.body.onload = 你好;
html html
body onload="hello()"正文 onload="你好()"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.