您需要将调用包装在一个通过引用分配的函数UpdatePoints()
中,而不是调用 in 中的函数。否则,您正在调用该函数,并将其返回值分配给.window.onload
window.onload
window.onload
// This function wraps UpdatePoints($test)
// and is assigned as a reference to window.onload
function load() {
UpdatePoints(<?php echo $test; ?>);
}
echo "<script type='text/javascript'> window.onload=load; </script>";
请注意,标签的language
属性<script>
已被弃用。在其位置包含一个type=text/javascript
属性(尽管 text/javascript 通常是浏览器的默认设置)
但是,由于 的值$test
是在页面加载之前创建的,并且在调用函数时无法更改,因此您最好不要将其作为参数传递,在这种情况下您不需要包装函数。只需删除()
即可将其分配为参考。
echo "<script type='text/javascript'> window.onload=UpdatePoints; </script>";
function UpdatePoints() {
// PHP write directly into the function since the value
// can't change... It's always going to be passed as what PHP assigns
// if you call it as UpdatePoints($test)
var Points = $test;
document.getElementById('PointsNumber').innerHTML = Points;
}