简体   繁体   English

防止加载jQuery

[英]Preventing jQuery from Loading

If jquery is added in globally used header.php across the site then How to stop to load jquery library only for those pages of site which doesn't need actually? 如果在整个站点的全局使用的header.php中添加了jquery,那么如何停止仅为那些实际不需要的站点页面加载jquery库? If we can't use more than one header. 如果我们不能使用多个标题。

purpose of question is to not to penalize those page with slow loading which actually don't need. 问题的目的是不惩罚那些实际上不需要的缓慢加载的页面。

Your site shouldn't need more than one global-header, if you opt to even use headers to begin with. 如果您选择甚至使用标头开头,那么您的网站不应该需要多个全局标头。 If it does, just include jQuery on all pages. 如果是这样,只需在所有页面上包含jQuery。 It's a small cached file, it won't hurt the browsing experience. 它是一个小的缓存文件,不会影响浏览体验。

By using the google-hosted version , it may be the case that many of your uses already have it cached before they even reach your site. 通过使用谷歌托管版本 ,可能会出现许多用户在访问您的网站之前已将其缓存的情况。

I have been guilty of pounding my fist into the nail while asking everyone else to move the hammer that's in the way... 我一直骂我的拳头钉在钉子上,同时要求其他人移动那个挡在路上的锤子......

Why not tackle the problem from the other end and use jQuery to optimize the first load? 为什么不从另一端解决问题并使用jQuery来优化第一次加载?

If you have big pages that are already taking a while to download, why not section off the less-performant areas and use $().load() to fill those in? 如果你有大页面已经花了一段时间下载,为什么不分离性能较低的区域并使用$()。load()来填充这些?

The page will load quicker (better user experience) and you don't have to be adding any additional processing to pages that don't need it. 该页面将加载更快(更好的用户体验),您不必向不需要它的页面添加任何其他处理。

Cheers, -jc 干杯,-jc

assuming you are loading the jQuery file from a correctly-configured webserver (or from google's CDN), it will be cached and not re-downloaded on each page. 假设您从正确配置的网络服务器(或谷歌的CDN)加载jQuery文件,它将被缓存,而不是在每个页面上重新下载。 Assuming a visitor will hit at least one page on your site that needs jQuery then you really won't gain anything by trying to remove it from loading on pages that don't use any javascript features from the library. 假设访问者将在您的网站上至少打到一个需要jQuery的页面,那么您真的不会通过尝试将其从加载到不使用库中任何javascript功能的页面上来获取任何内容。

First, use the compressed jquery for production. 首先,使用压缩的jquery进行生产。 It's much smaller. 它要小得多。 Second, IIRC, once jquery is downloaded with the first page, it will be cached and won't need to be retrieved from your server for each subsequent request. 其次,IIRC,一旦jquery与第一页一起下载,它将被缓存,并且不需要从服务器检索每个后续请求。

Otherwise, if you really need to explicitly load jquery only on those pages that need it, you would have to have some way for the body of your page to tell header.php that it doesn't need to load jquery. 否则,如果你真的只需要在那些需要它的页面上显式加载jquery,你必须有一些方法让页面正文告诉header.php它不需要加载jquery。 If header.php is loaded before "body.php" then that's pretty hard to do without some fancy output buffering or such. 如果在“body.php”之前加载header.php,那么没有一些花哨的输出缓冲等就很难做到。

If you're using a templating system like Smarty you could have a conditional in the master page template to check a $loadjquery flag that you set in body.php before sending the whole page off to be rendered. 如果你正在使用像Smarty这样的模板系统,你可以在母版页模板中有条件来检查你在body.php中设置的$ loadjquery标志,然后再发送整个页面进行渲染。 But that's complicated too. 但这也很复杂。

Your question is very general, some specific would be great, maybe even a link to the site. 你的问题非常笼统,有些具体可能很棒,甚至可能是网站的链接。 Personally if you are using a CMS I would try to make some sort of "flag" for that page, or if you are simply loading a page and then loading the header from that page, insert a variable before you load the header and use that as your flag for loading jQuery. 就个人而言,如果您使用CMS,我会尝试为该页面制作某种“标志”,或者如果您只是加载页面然后从该页面加载标题,请在加载标题之前插入变量并使用该标题作为加载jQuery的标志。

An example: If a user wants to see www.mysite.com then the following file would be loaded: www.mysite.com/index.php with the following code: 示例:如果用户想要查看www.mysite.com,则会加载以下文件:www.mysite.com/index.php,其中包含以下代码:

<?php $needJQuery = true;
include('header.php');
echo 'content will go here';
include('footer.php'); ?>

header.php would include something such as this: header.php会包含如下内容:

<?php if ($needJQuery) { ?>
<script src="/jquery/jquery-min-3.2.1.js" />
etc. for all the content that you need above/below.
<?php } ?>

For the pages that don't need jQuery loaded, you would either leave $needJQuery undefined or you would do as follows: 对于不需要加载jQuery的页面,您可以将$ needJQuery保留为undefined,或者您将执行以下操作:

<?php $needJQuery = false; ?>

Hope this helps, 希望这可以帮助,

As stated earlier, modify the header file so it'll check for the presence of flag variable and only output the jquery headers if needed. 如前所述,修改头文件,以便检查是否存在flag变量,并在需要时仅输出jquery头。

If you don't have the ability to modify the header file, you could load it with output buffering turned on and filter the output before it heads out to the client: 如果您无法修改头文件,可以在打开输出缓冲的情况下加载它并在输出到客户端之前过滤输出:

<?php
ob_start();
include('header.php');
$header = ob_get_flush();
$cleanheader = some_operation_that_removes_the_jquery_script_tags($header);
echo $cleanheader
?>

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

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