Magento - 如何逐页包含 javascript 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3937837/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 06:43:56  来源:igfitidea点击:

Magento - how to include javascript file on a page by page basis

javascriptxmllayoutmodulemagento

提问by Jon

In magento I know how to include js files in the page layout file. How though, do i include specific javascript files only on certain pages with my custom module. For example i am writing a custom module that will work with the product view and list pages. I therfore want to have some sort of layout update that i can use with my module that will include my javascript file only on the product view and list pages.

在 magento 中,我知道如何在页面布局文件中包含 js 文件。但是,我是否仅在具有自定义模块的某些页面上包含特定的 javascript 文件。例如,我正在编写一个自定义模块,它将与产品视图和列表页面一起使用。因此,我想要某种布局更新,我可以将其与我的模块一起使用,该更新将仅在产品视图和列表页面上包含我的 javascript 文件。

回答by Drew Hunter

you will need to add a layout update section to your modules config file. based on what you have said you will need something like this in your layout file:

您需要在模块配置文件中添加布局更新部分。根据您所说的,您的布局文件中将需要这样的内容:

<?xml version="1.0"?>

<layout version="0.1.0">
    <catalog_product_view>
         <reference name="head">
                <action method="addJs"><script>yourscript.js</script></action>
            </reference>
    </catalog_product_view>

    <catalog_category_view>
           <reference name="head">
                <action method="addJs"><script>yourscript.js</script></action>
            </reference>
    </catalog_category_view>

</layout>

Then in your modules config file, you will need something like:

然后在您的模块配置文件中,您将需要如下内容:

<frontend>
    <layout>
        <updates>
           <yourmodule>
               <file>yourlayout.xml</file>
            </yourmodule>
         </updates>
     </layout>
 </frontend>

This assumes that yourscript.js is sitting in the root of the js folder. Obviously, you dont want to put it here so do what Jonathan advised and use:

这假设 yourscript.js 位于 js 文件夹的根目录中。显然,你不想把它放在这里,所以按照乔纳森的建议和使用:

 <action method="addItem"><type>skin_js</type><name>path/file.js</name></action>

and put your js in your themes skin folder.

并将您的 js 放在您的主题皮肤文件夹中。

Good luck!

祝你好运!

回答by Jonathan Day

What you need to do is find the layout handles for the pages that you want to include the JS, and add them to your custom module's layout XML. For instance, to include the JS on the product view, you would add the following node to your layout:

您需要做的是找到要包含 JS 的页面的布局句柄,并将它们添加到自定义模块的布局 XML 中。例如,要在产品视图中包含 JS,您需要将以下节点添加到您的布局中:

<catalog_product_view>
  <reference name="head">
    <action method="addJs"><script>path/file.js</script></action>  **OR**
    <action method="addItem"><type>skin_js</type><name>path/file.js</name></action>
  </reference>

Choose either option depending on where your JS files live. Have a look in the default catalog.xml or review.xml for examples.

根据您的 JS 文件所在的位置选择任一选项。查看默认的 catalog.xml 或 review.xml 以获取示例。

To work out the layout handle (e.g. catalog_product_view), you can use Alan Storm's excellent (and free) LayoutViewer module from his blog.

要计算布局句柄(例如 catalog_product_view),您可以使用 Alan Storm 的博客中出色(且免费)的 LayoutViewer 模块。

HTH, JD

HTH, JD