How to call external javascript on window.load?

I want to post this on 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 do not want to put it in the index, but in a separate file, say hello.js

How can I call it from an index file, so when I click on index.html it immediately asks for my name. (eg)

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

+5
source share
4 answers

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 <script src="hello.js"></script>should work fine.

+6
source

, script. script . , , . javascript. , , , . , HTML <!--, ...

+1

- :

<script type="text/javascript"> 
var name = prompt("What is your name", "");
alert ( "Hello " + name + "! Welcome to my forum.");  </script>

i.e, , .

0

Ubuntu 16.04

JS

change-from: → window.onload = hello;

change-to: → window.document.body.onload = hello;

HTML

body onload = "hello()"

-1

All Articles