如何在html页面加载后执行php代码

时间:2017-10-22 07:47:51

标签: javascript php

完整的html页面加载后想要执行一些php api& s&脚本和显示响应

<html>
<body>
content 
<?php
//some php script execute after full page load
?>
</body>
</html>

2 个答案:

答案 0 :(得分:3)

为此,您可以在呈现页面后使用Ajax调用,

$(window).load(function(){ /*This allows to execute your code after the  page is rendered fully */ 
    $.get("<path to php file>", function(data){
     /* you can then process the ajax response as needed */
    });
})

您可以(Javascript that executes after page load)在页面加载后加载脚本

答案 1 :(得分:1)

PHP是一种服务器端编程语言。它会在您请求页面时执行。因此,在加载页面后,您无法执行它。 如果您需要检查客户端是否已加载某些内容,则必须使用JavaScript等客户端编程语言。

通过这种方式,您可以向PHP文件(将被执行)发出请求,并处理该文件返回的所有内容。

您可以使用JavaScript或jQuery(JavaScript库)执行此操作:

<script type="text/javascript" src="jquery.js"></script>          
<script type="text/javascript">
    // Check if the page has loaded completely                                         
    $(document).ready( function() { 
        $('#some_id').load('php-file.php'); 
    }); 
</script>