简体   繁体   English

页面加载后如何触发Javascript函数?

[英]How do I trigger a Javascript function AFTER the page loads?

** EDIT ** **编辑**

I'm afraid I wasn't in the right direction - the problem isn't what I asked about. 恐怕我没有朝着正确的方向前进-问题不是我问的。 the Javascript works as it should, it's the PHP that doesn't show what I want it to on the first "run" - and I'm stil not sure why. Javascript可以正常工作,它是PHP不能在第一个“运行”中显示我想要的内容-我不确定为什么。 Sorry for somewhat wasting your time... 抱歉浪费您的时间...

** **

I have a form that might or might not already contain data in its fields. 我有一个表单,该表单可能会或可能不会已经在其字段中包含数据。 If a user reaches the form and he already has some data in the system, his ID number, for instance, will appear in the ID field - and a JavaScript function running onLoad will disable the input field and change its style: 如果用户到达表单并且他已经在系统中拥有一些数据,那么他的ID号(例如)将出现在ID字段中-运行onLoad的JavaScript函数将禁用输入字段并更改其样式:

<body onload="runFunction()">

the problem is that it seems to run before the PHP does its part, and only if I refresh the page, the JS function does everything I want it to (it disables and styles some other form fields that depend on a PHP condition). 问题是它似乎在PHP发挥作用之前就运​​行了,并且只有刷新页面后,JS函数才能完成我想要的一切(它会禁用和设置依赖于PHP条件的其他表单字段的样式)。

Is there a way to have this JS function run AFTER the page was rendered? 有没有办法让此JS函数在呈现页面后运行? I tried running this before closing the page: 我在关闭页面之前尝试运行此命令:

<body>
...
...
<script>
runFunction();
</script>
</body>
</html>

but to no avail. 但无济于事。

any ideas? 有任何想法吗? Thanks! 谢谢!

some of the relevant PHP code: [I removed id attributes to make reading easier] 一些相关的PHP代码:[我删除了id属性以使阅读更容易]

<?php if ($h_phone != '' && $h_phone_pre != '') {  
echo "<td class=\"input\"><input type=\"text\" id=\"new_user_home_tel\" value=\"$h_phone\" size=\"8\" maxlength=\"7\" disabled=\"disabled\" /> -  
<select id=\"new_user_home_tel_pre\" disabled=\"disabled\">  
  <option value=\"$h_phone_pre\" selected=\"selected\"></option>";

     } else {

echo '<td class="input"><input type="text" id="new_user_home_tel" size="8" maxlength="7" /> -  
 <select id="new_user_home_tel_pre">  
   <option value=" " selected="selected"></option>';
    }?>  <option value="02">02</option>
     <option value="03">03</option>
     <option value="04">04</option>
     <option value="08">08</option>
     <option value="09">09</option>
</select> 
</td>   

the Javascript code just changes the style if the field isn't empty and that works, but the PHP works only after a refresh. 如果字段不为空,则Javascript代码仅更改样式,并且可以使用,但是PHP仅在刷新后才能使用。

Your Javascript does run after the page is loaded, the problem is elsewhere. 您的Javascript确实在页面加载后运行,问题出在其他地方。

It must be that your server sends different HTML before and after refresh. 必须是您的服务器在刷新前后发送不同的HTML。 I suggest that if you save the source of the page you get first time and compare it with the source you get after refresh. 我建议,如果您保存页面的源代码,则将是第一次,并将其与刷新后的源代码进行比较。 I bet you will spot the difference and that will tell you what is wrong. 我敢打赌,您会发现差异,这将告诉您哪里出了问题。

JQuery does this. jQuery可以做到这一点。 Anything inside the following will execute once the page is rendered: 呈现页面后,以下内容将立即执行:

$(document).ready(function() {
  //Call your function here
});

I know this is really late for this post, but the reason this isn't working is that PHP renders HTML and then you can't interact with it again (unless you can do some snazzy AJAX stuff). 我知道这篇文章真的很晚了,但是不起作用的原因是PHP呈现了HTML,然后您无法再次与之交互(除非您可以做一些时髦的AJAX事情)。 Point being that the JS works fine bc it is client side, PHP doesn't work bc it is server side. 要点是,JS在客户端方面可以很好地工作,而PHP在服务器端方面则不能工作。 Hope that helps anyone else who comes here! 希望对任何来这里的人有所帮助!

尝试“延迟”属性。

One way is to output-buffer the page in PHP. 一种方法是在PHP中输出缓冲页面。 That means everything generated goes into memory until the script finishes running and then it's all sent out at once. 这意味着生成的所有内容都会进入内存,直到脚本完成运行,然后全部发送出去。

Take a look at http://uk.php.net/ob_start 看看http://uk.php.net/ob_start

the non JS library answer is: 非JS库的答案是:

<script>
window.onload = function(){
 /* do stuff */
}
</script>

However using a JS library like JQuery will take care of all those niggle cross browser bug/problems. 但是,使用像JQuery这样的JS库将解决所有那些讨厌的跨浏览器错误/问题。

Your right to place your <script> blocks at the end of you body. 您将<script>块放置在身体末端的权利。 This improves page load performance as the browser blocks when it hits a <script> block. 当浏览器碰到<script>块时,它会提高页面加载性能。

Definitely hacky, but you could always put a 2x2 transparent image below your form and run the function using it's onLoad. 绝对是hacky,但是您始终可以在表单下方放置一个2x2透明图像,并使用onLoad运行该函数。 I did a similar thing on a conditionally included file so there were no errors in the case that the file wasn't included. 我对有条件包含的文件执行了类似的操作,因此在不包含文件的情况下没有错误。

I'd suggest that you add your content to an output variable and than echo it at the end of the file. 我建议您将内容添加到输出变量,然后在文件末尾回显它。 Ex: 例如:

...
$output .= '<body onload="runFunction();">';
while(...)
{
   $output .= '...';
}
$output .= '</body>';
...
echo $output;

I'm afraid I wasn't in the right direction - the problem isn't what I asked about. 恐怕我没有朝着正确的方向前进-问题不是我问的。 the Javascript works as it should, it's the PHP that doesn't show what I want it to on the first "run" - and I'm stil not sure why. Javascript可以正常工作,它是PHP不能在第一个“运行”中显示我想要的内容-我不确定为什么。 Sorry for somewhat wasting your time... 抱歉浪费您的时间...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM