How to add/include js file into Magento theme

前端 未结 9 1016
予麋鹿
予麋鹿 2020-12-14 23:34

I am modifying a Magento theme. And want to add a js file into the theme /js folder. I have added the following code:

相关标签:
9条回答
  • 2020-12-15 00:05

    Try adding the following to your layout .xml file within <reference name="head">

    <action method="addJs">
        <script>js/custom-script.js</script>
    </action>
    
    0 讨论(0)
  • 2020-12-15 00:08

    I believe if you change:

    <action method="addItem"><type>skin_js</type><name>js/custom-script.js</name></action>
    

    to

    <action method="addItem"><type>skin_js</type><name>skin/frontend/{Theme Package Name}/{Theme Name}/js/custom-script.js</name></action>
    

    that should allow you to access theme specific javascript file.

    0 讨论(0)
  • 2020-12-15 00:10

    If you want to include javascript inthemethen put this code inyour module's layout.xmlunderdefault` tag.

    <layout>  
     <default>
        <reference name="head">
            <action method="addJs">
                <script>custom-script.js</script>
            </action>
        </reference>
    </default>
    </layout>
    

    If you want to include javascript for any particular controller then put this code in your module's layout.xml like below

    <layout>  
    <yourpackage_yourmodule_yourcontroller_action translate="label" module="yourpackage_yourmodule">       
        <reference name="head">
            <action method="addJs">
                <script>custom-script.js</script>
            </action>
        </reference>
    </yourpackage_yourmodule_yourcontroller_action>
    </layout>
    

    And put custom-script.js file under yourMagentoDirectory/js folder.

    0 讨论(0)
  • 2020-12-15 00:13

    Add this code in,

    app/design/frontend/{your_theme}/default/template/page/html/header.phtml

    <script type="text/javascript" src="<?php echo $this->getSkinUrl(); ?>js/custom-script.js"></script>
    

    And put custom-script.js file in js folder on root.

    FYI : Like this you can add any js/css file on phtml files

    0 讨论(0)
  • 2020-12-15 00:18

    try with this code:

        <default>
            <reference name="head">
                <action method="addJs"><script>js/jquery/jquery-1.7.2.min.js</script></action>
            </reference>
        </default>
    

    Regards

    0 讨论(0)
  • 2020-12-15 00:19

    If you want to implement custom code (a .js file), please follow these steps:

    • From the Admin panel, go to Design > Themes Editor.

    • Click Customize under the theme you wish to add custom script to.

    • Click Java Script Editor in the left panel to manage JavaScript assets.

    • Click Browse Files, select the JavaScript file from your local drive, and then click Upload Files.

    0 讨论(0)
提交回复
热议问题