I have the following problem:
JavaScript
x
11
11
1
<body>
2
<script>
3
function myfunction(y) {
4
alert(y);
5
}
6
7
var x = "1";
8
myfunction(x);
9
</script>
10
</body>
11
This script gives me the desired result: 1 However, the script is executed in my code only with document.body.onload:
JavaScript
1
11
11
1
<body>
2
<script>
3
document.body.onload = function myfunction(y) {
4
alert(y);
5
}
6
7
var x = "1";
8
myfunction(x);
9
</script>
10
</body>
11
But in this case the result is not 1, but [object Event]. What do I have to do to get 1 as result?
Advertisement
Answer
I’m not sure if this is what you mean:
JavaScript
1
8
1
function myfunction(y) {
2
alert(y);
3
}
4
document.body.onload = function() {
5
var x = "1";
6
myfunction(x);
7
}
8
Basically, if you set onload
as you function which takes a parameter – the argument will be event itself, not the value you tried to provide to it
You can read more here