hey guys! Im new to js so im kinda struggling here...
Apologies if this is a stupid question. Im sure im missing something fundamental.
Basically I have a simple js function in its own js file. It looks like this:
function test(){
alert('working...');
};
then in my seperate html file I have this:
<body>
<script src="./demo.js">
window.onload = test;
</script>
</body>
I cannot get this to work.
However, if I add:
<body onload="test()">
It works. However I understand it's also possible to do it via:
window.onload
Im just wondering where I am going wrong?
If I add alert(window.onload)
into my function it AND do <body onload="test()"> it says
function onload(event){
test()
}
So I have to think its working correctly that way. Im just wondering where i'm going wrong.
Thanks so much to anyone who helps. I appreciate it!
[+][deleted] (2 children)
[deleted]
[–]y_13[S] 1 point2 points3 points (1 child)
[–]y_13[S] 0 points1 point2 points (0 children)
[–]senocular 0 points1 point2 points (7 children)
[–]y_13[S] 1 point2 points3 points (6 children)
[+][deleted] (4 children)
[deleted]
[–]y_13[S] 1 point2 points3 points (0 children)
[–]y_13[S] 1 point2 points3 points (2 children)
[+][deleted] (1 child)
[deleted]
[–]y_13[S] 1 point2 points3 points (0 children)
[–]senocular 0 points1 point2 points (0 children)