0
I have two javascript functions that should be executed when loading the page in PHP. Both are similar. Here’s one of them:
<?php
if ($idAluno == $idRespA) {
echo '<script> window.onload = function(){ funcaoA() }; </script>';
} else {
$respA = $db->select('tbl_pessoas',['*'],['id'=>$idRespA]);
}
?>
The second part is the same thing. It only changes the IF, which compares with another variable and if it is closed runs the function B(). But it’s pretty much the same thing. Both are executed at page startup, one after the other.
The problem is that when running it only works the 'window.onload' of one of them. So I guess this isn’t the best way to run a JS on startup.
What a way to run JS inside Ifs in PHP on page startup?
I was also told that it is not good to run JS functions directly in PHP. Why?
Tb did not understand why not run a JS function in PHP, since one thing has nothing to do with another. The
echo
is nothing more than returning an HTML.– Sam
The
onload
will only be loaded once. You can create a variable, for example:$script[] = " funcaoA(); "
and$script[] = "funcaoB();"
and then just useecho "<script>window.onload =
 function() { ".implode("", $script)." }</script>"
. But avoid doing this. This is gambiarra and may cause you problems in the future.– Valdeir Psr
place
window.onload = function(){ funcaoA(), funcaoB() }
– Sam