jQuery .load()运行与显示的身体相同
我有一个项目,需要将外部HTML包含在我的页面中。因此,此文件将包含在许多页面中。我在这里使用jQuery。让我显示一些代码:
$(document).ready(function(){
$('myBody').load('myExternal.html');
});
当然可以运行...但是在加载我的页面后,该外部文件已加载。我想要的是,当我的页面加载后,外部文件也直接被删除,而无需等待时间。有人可以帮忙吗?感谢您的所有帮助
i have a project that needs to include external html to my page. so this file will be included to many page. I'm using jQuery here. Let me show some code:
$(document).ready(function(){
$('myBody').load('myExternal.html');
});
It runs of course... But that external file is loaded AFTER my page has been loaded. What i want is, when my page has been loaded, the external file is dispayed too ,directly, without wait some time. Anyone can help? I appreciate all of your helps
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
延迟外部文件的原因是因为您仅触发
.load()
功能在整个基本文档已经加载并准备就绪之后。这意味着您的外部文件至少在请求的时间之前将迟到,以及jQuery或其他任何东西的任何开销。
jQuery
.dready()
方法“提供了一种运行JavaScript的方法该页面的文档对象模型(DOM)一旦可以安全地操纵,代码就会立即。”这意味着,当您的加载功能运行时,该文档已显示给用户,您必须等待浏览器获取外部文件。使用AJAX客户端加载页面部分可能会导致缓慢,笨拙或无法使用的网页,因为用户需要等待这些部分才能使用网站。此外,由于多种原因,这些部分可能根本不加载。许多以隐私为重点的浏览器禁用JavaScript,一些较旧的浏览器可能不支持您的代码使用的功能,客户端的互联网可能是斑点或其他任何内容。将整个页面作为服务器基本文档的一部分要好得多。
也就是说,请查看 apache Servers> =“ http://nginx.org/en/docs/http/ngx_http_ssi_module.html” rel =“ nofollow noreferrer”> nginx for nginx (如果您使用的话))。这不是JavaScript解决方案,而应该完成您想要的。该部分将在文件离开服务器之前,节省大量的开销时间和可能的问题,而不是在运行时加载客户端。
如果您真的想为此使用JavaScript,请考虑显示“请等待”,直到加载整个页面(包括外部部分)。之后,删除“请等待”并显示页面。
The reason your external file is being delayed is because you're only firing the
.load()
function after the entire base document is already loaded and ready. That means your external file will be late by at least the time of the request, plus any overhead from jQuery or anything else.The jQuery
.ready()
method "offers a way to run JavaScript code as soon as the page's Document Object Model (DOM) becomes safe to manipulate." That means when your load function runs, the document has been displayed to the user, and you'll have to wait for the browser to get the external file.Loading page sections using Ajax client-side can result in slow, cumbersome, or unusable web pages, because the user needs to wait for those sections before they can use the website. Additionally, the sections may just not load at all, for any number of reasons. Many privacy-focused browsers disable Javascript, some older browsers might not support features your code is using, the client's internet could be spotty, or anything else. Having the entire page as part of the base document from the server is much better.
That said, look into Apache Server-Side Includes (equivalent for nginx, if you use that). This isn't a Javascript solution, but should accomplish what you want. Instead of the client loading them at runtime, the sections will be included before the file leaves the server, saving on a ton of overhead time and possible issues.
If you really want to use Javascript for this, consider displaying a "please wait" until the entire page, including external sections, is loaded. After that, remove the "please wait" and show the page.