如何在页面加载时使用 ajax 将 php 包含加载到页面中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16933989/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I load a php include into a page with ajax whilst the page is loading
提问by Ben Paton
I have a php include which takes awhile to load because PHP has to fetch a lot of data. I don't want to slow the whole webpage loading waiting for this include so how can I load this one include with ajax? I don't want the ajax to be triggered by a button click I just want it to load up the include as the page is loading so that if you look at my example below 'Some more html content' would be displayed whilst the inlude.php is still loading in.
我有一个 php 包含需要一段时间才能加载,因为 PHP 必须获取大量数据。我不想减慢整个网页的加载速度以等待此包含,那么如何使用 ajax 加载此包含?我不希望通过单击按钮触发 ajax 我只希望它在页面加载时加载包含,这样如果您查看下面的示例,将在插入时显示“更多 html 内容”。 php 还在加载中。
<html>
<head>
</head>
<body>
Some html content
<script>
Ajax load 'include.php';
</script>
Some more html content
</body>
</html>
回答by user2444499
If you're using jQuery, you could you use one of their ajax calls to load your html from include.php. For example you could use the load()function.
如果您使用 jQuery,您可以使用他们的 ajax 调用之一从 include.php 加载您的 html。例如,您可以使用load()函数。
For example:
例如:
<div id="content"></div>
<script>
$(document).ready(function() {
$("#content").load("/yourpath/include.php");
});
</script>
回答by eric_zyh
use jquery , load php after DOM ready (before they render)
使用 jquery ,在 DOM 准备好后(在渲染之前)加载 php
<div id="include"></div>
$(function(){
$("#include").load("include.php");
});
回答by Robert Seddon-Smith
If you are not using jQuery, you could trigger your AJAX function using document.onload. I have never tried this to be fair as most PHP is lightning fast and I'd worry about what would happen if the page only partially loaded. You could use document.ready to avoid this, but then you're just waiting for the page to load. Remember you have limited connections per browser per server by HTTP so you may find this does not speed anything up in the end.
如果您不使用 jQuery,您可以使用 document.onload 触发您的 AJAX 函数。我从来没有试过这样公平,因为大多数 PHP 都快如闪电,我担心如果页面只是部分加载会发生什么。您可以使用 document.ready 来避免这种情况,但是您只是在等待页面加载。请记住,每个服务器的每个浏览器通过 HTTP 的连接是有限的,因此您可能会发现这最终不会加快任何速度。
I use some pretty technical pages and occasionally they take as long as 0.06 seconds to load though usually they are quicker. This on a very cheap and nasty server with extremely scant resources. Users cannot perceive this as a lag as it takes much longer to download the content than PHP takes to serve it.
我使用一些非常技术性的页面,有时它们需要长达 0.06 秒的时间来加载,但通常它们会更快。这是在资源极其匮乏的非常便宜和讨厌的服务器上进行的。用户无法将其视为延迟,因为下载内容所需的时间比 PHP 提供的时间长得多。
Ask yourself:
问你自己:
- List item Why is my code taking so long to load?
- List item Do I need all that code in the include?
- List item Is there a better way?
- List item Should I remove infrequently used code to separate includes (or use the class batch loading facility in PHP)?
- 列表项 为什么我的代码加载时间这么长?
- 列表项 我需要包含中的所有代码吗?
- List item 有没有更好的方法?
- 列表项 我是否应该删除不常用的代码来分隔包含(或使用 PHP 中的类批量加载功能)?
Perhaps it might be better to simplify your code so it runs faster. If you don't intend to serve all that data you are fetching, perhaps you don't need to fetch it at all. If you are going to serve it, that is what is going to take the time, not the processing.
也许简化您的代码以使其运行得更快可能会更好。如果您不打算提供您正在获取的所有数据,也许您根本不需要获取它。如果您要为它提供服务,那就是需要时间,而不是处理。