简体   繁体   English

如何检查php网站是否完全加载了JS

[英]How to check if php website was completely loaded with JS

So I have an index.php, where inside I have some Div's like this: 所以我有一个index.php,里面我有一些像这样的Div:

<div id="thisDiv">
    <?php ?>
<div>

and in another JS file, I have a divRefresh function like this: 在另一个JS文件中,我有一个divRefresh函数,如下所示:

function divRefresh(id, path){
    $(id).load(path, function(){
        functionToBeCalledWhenLoaded();
    });
}

where ID would be the id from the div, and PATH the path to where the php file is in the server. 其中ID是div的id,PATH是php文件在服务器中的路径。

My problem is that the load() function calls the functionToBeCalledWhenLoaded() before even my .php page was completelly loaded. 我的问题是load()函数甚至在我的.php页面被完全加载之前调用了functionToBeCalledWhenLoaded()

Solutions tried: 解决方案:
callback functions. 回调函数。
inline <script> call. 内联<script>调用。 (this solution worked with only some people) (此解决方案仅适用于某些人)
jquery ready() , onload() , load() jquery ready()onload()load()

How do I check if my php page was completelly loaded before calling a JS function? 在调用JS函数之前,如何检查我的php页面是否已完全加载?

It seems that the DOM is ready while the PHP server still sends data. 似乎DOM已准备好,而PHP服务器仍然发送数据。 Here you go some suggestions that may convince the browser to understand the situation. 在这里,您可以提出一些可能说服浏览器了解情况的建议。 First, try to turn it off and on again. 首先,尝试将其关闭再打开。 If it doesn't help: 如果它没有帮助:

Suggestion 1 建议1

Try to send header("Content-Length: ...") - it may convince the browser to wait until all data are sent. 尝试发送header("Content-Length: ...") - 它可能说服浏览器等到所有数据都发送完毕。 The ... is the byte size of the generated HTML, of course. 当然,...是生成的HTML的字节大小。

Suggestion 2 建议2

Try to use output buffering: 尝试使用输出缓冲:

<?php ob_start();?>
<div id="thisDiv">
    <?php ?>
<div>
<?php ob_end_flush();?>

The server will wait until the html is fully generated and then it will send it to the client, so the last data would be ...</html> , after which the DOM will be ready and after that the JS load will launch. 服务器将等到html完全生成然后它将它发送到客户端,所以最后的数据将是...</html> ,之后DOM将准备就绪,之后JS负载将启动。 At least I hope so. 至少我希望如此。

Suggestion 3 建议3

At the end of the php code which you put inside thisDiv , insert this line 在放入thisDiv的php代码的thisDiv ,插入此行

<img src="pixel.gif" onload="functionToBeCalledWhenLoaded()">

Where the pixel.gif is some real 1px invisible image, so the function will launch after the php part is done. 其中pixel.gif是一些真正的1px不可见图像,因此该函数将在php部分完成后启动。 This could work when the DOM is ready and the relevant data are sent, while the browser waits for some more data. 当DOM准备好并发送相关数据时,这可能会起作用,而浏览器会等待更多数据。


Last, but not least, it could be some cache issue. 最后,但并非最不重要的,它可能是一些缓存问题。 As the last resort, you could use setTimeout(functionToBeCalledWhenLoaded,1000) to wait another second and hope that the page will be really ready after the delay. 作为最后的手段,你可以使用setTimeout(functionToBeCalledWhenLoaded,1000)等待一秒钟,并希望在延迟之后页面真的准备就绪。 This would be a nice example of duct-tape antipattern, an act of sheer despair. 这将是一个很好的塑料管道反模式的例子,纯粹的绝望行为。

If you use jQuery you can put the code between: 如果你使用jQuery,你可以把代码放在:

$(document).ready(function() { } or the shorthand version $(function(){ } $(document).ready(function() { }或简写版本$(function(){ }

If you're not using jquery: 如果你不使用jquery:

window.onload = function(){ }

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

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