Wan Wan - 5 months ago 16
HTML Question

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

How can I call it from index file so when I click the index.html, it will immediately prompt for my name. (for example)

I put
<script src="hello.js"></script>
does not work.

Answer

Your hello.js should look something like this:

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.

Comments