【问题标题】:how move javascript to bottom footer in drupal如何在drupal中将javascript移动到底部页脚
【发布时间】:2011-06-21 21:55:41
【问题描述】:

drupal 中的性能问题

如何将 javascript 移动到 tpl 文件的底部页脚??

【问题讨论】:

    标签: javascript performance drupal footer


    【解决方案1】:

    在您主题的page.tpl.php 中,将print $scripts; 行移至页脚。一些模块的 JS 代码不喜欢这样,但我已经使用了大多数。

    http://groups.drupal.org/node/8399

    【讨论】:

    • 谢谢,但我想不影响任何js文件,但我读到有一种方法可以将默认位置更改为核心drupal中的页脚,但没有详细信息,如何???
    • 我想您可以调整 drupal_add_js 以使用不同的默认 $scope,但修改核心通常是个坏主意。无论如何,您对第三方模块也会有同样的问题,因为它们通常依赖于默认的“标题”。
    【解决方案2】:

    我刚刚在 Drupal Answers 上回答了一个类似的问题:https://drupal.stackexchange.com/questions/46202/move-aggregated-js-file-to-the-footer/89590#89590

    我复制粘贴下面的答案以供快速参考。

    另外,我不确定是否应该将问题迁移到 Drupal Answers 或合并,否则如果有人有想法,请执行此操作或建议如何操作。谢谢。


    为 Drupal 7 找到了这个出色的代码 sn-p:https://gist.github.com/pascalduez/1418121

    它提供了一种拥有 $script 和 $head_scripts 的方法,以便您可以指定哪些 JS 文件需要放在 head 中。例如,Modernizr 应该进入头部脚本。

    我将复制粘贴到链接中的解决方案下方,以便将来证明答案。

    干杯。

    html.tpl.php

    <!DOCTYPE html>
    <html<?php print $html_attributes; ?>>
    <head>
    <?php print $head; ?>
    <title><?php print $head_title; ?></title>
    <?php print $styles; ?>
    <?php print $head_scripts; ?>
    </head>
    
    <body<?php print $body_attributes;?>>
    <?php print $page_top; ?>
    <?php print $page; ?>
    <?php print $scripts; ?>
    <?php print $page_bottom; ?>
    </body>
    </html>
    

    template.php

    // Used in conjunction with https://gist.github.com/1417914
    
    /**
    * Implements hook_preprocess_html().
    */
    function THEMENAME_preprocess_html(&$vars) {
    // Move JS files "$scripts" to page bottom for perfs/logic.
    // Add JS files that *needs* to be loaded in the head in a new "$head_scripts" scope.
    // For instance the Modernizr lib.
    $path = drupal_get_path('theme', 'THEMENAME');
    drupal_add_js($path . '/js/modernizr.min.js', array('scope' => 'head_scripts', 'weight' => -1, 'preprocess' => FALSE));
    }
    
    /**
    * Implements hook_process_html().
    */
    function THEMENAME_process_html(&$vars) {
    $vars['head_scripts'] = drupal_get_js('head_scripts');
    }
    

    【讨论】:

      【解决方案3】:

      将所有 Drupal 的 JS 移动到页脚的最快方法是将 $scripts 移动到结束正文标记之前和 之前 $closure

      在 Drupal 6 内核中,没有您可以轻松检查的选项或界面。但是在admin/settings/performance 下有一个压缩 JS 文件的选项。推荐用于生产。

      要将您的 JS 文件放在底部,请转到您的 page.tpl.php 文件并查找 &lt;?php print $scripts;?&gt; 或类似的东西。然后寻找$closure;并更改它:

          <!-- other theme code above -->
          <?php print $scripts; ?>
          <?php print $closure; ?>
          </body> 
      </html> 
      

      【讨论】:

        【解决方案4】:

        【讨论】:

          【解决方案5】:

          我发现移动了

          print $scripts
          在底部很少起作用。大多数时候,您需要一个解决方案,允许将一些脚本保留在页眉中,而其他脚本可以移动到页脚中。

          就个人而言,我在 template.php 中使用 drupal_add_js 以利用范围选项。

          来自 Drupal 文档:

          scope:您要放置脚本的位置。
          可能的值是“header”或“footer”。
          如果您的主题实现不同的区域,您也可以使用这些。
          默认为“header” '。

          【讨论】:

            猜你喜欢
            • 2021-06-07
            • 2010-09-23
            • 2021-11-08
            • 2012-11-21
            • 1970-01-01
            • 2014-12-24
            • 2016-09-11
            • 1970-01-01
            • 2022-12-05
            相关资源
            最近更新 更多