最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Php calling javascript on load - Stack Overflow

programmeradmin7浏览0评论

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

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

Share Improve this question asked Oct 25, 2012 at 1:39 Veljko89Veljko89 1,9533 gold badges30 silver badges49 bronze badges 8
  • "when page load i do little calculation with PHP stuff" --- actually all the calculations are already done when page is loading – zerkms Commented Oct 25, 2012 at 1:40
  • that is correct ... was just grammar mistake – Veljko89 Commented Oct 25, 2012 at 1:41
  • @zerkms that is usually true but it is possible to flush the data before the script is finished executing - though in this case that is not happening. – HellaMad Commented Oct 25, 2012 at 1:42
  • What is not working? Is there an error message? – HellaMad Commented Oct 25, 2012 at 1:43
  • 1 @zerkms Agreed, just pointing that out for future viewers. – HellaMad Commented Oct 25, 2012 at 3:10
 |  Show 3 more ments

1 Answer 1

Reset to default 2

Instead of calling the function UpdatePoints() in window.onload, you need to wrap the call in a function that gets assigned by reference to window.onload. Otherwise, you are calling the function, and assigning its return value to 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>";

Note that the language attribute to the <script> tag is deprecated. Include a type=text/javascript attribute in its place (though text/javascript is genearlly the browser default)

However, since the value of $test is 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.

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;
}
发布评论

评论列表(0)

  1. 暂无评论