Guys i have little problem with PHP to call Javascript function on load …
Idea is when page load i do little calculation with PHP stuff , so when i finish that all i
want is to write that down in same DOM element using javascript.
I will not show you PHP code for calculation as i am 100% sure it is alright.
This is the code i got so far , so can you just tell me whats wrong with it?
$test = 100;
echo "<script language=javascript> window.onload=UpdatePoints($test); </script>";
and Javascript function is simple
function UpdatePoints(Points) {
document.getElementById('PointsNumber').innerHTML = Points;
}
Thanks in advance
Instead of calling the function
UpdatePoints()inwindow.onload, you need to wrap the call in a function that gets assigned by reference towindow.onload. Otherwise, you are calling the function, and assigning its return value towindow.onload.Note that the
languageattribute to the<script>tag is deprecated. Include atype=text/javascriptattribute in its place (though text/javascript is genearlly the browser default)However, since the value of
$testis created before the page loads and cannot change when the function is called, you might as well not bother passing it as a parameter, in which case you don’t need to wrap the function. Just remove the()to assign it as a reference.