first, the global invocation method:
By this method each page will refer to this JS file, unless it is similar to the jquery system files, otherwise this method is not recommended.
File path:/app/design/frontend/default/your_template/layout/page.xml
You'll see a lot of XML code like ADDJS, which is one of the advantages of Magento, and it's easy and flexible to configure files through XML.
As follows:
XML code
- <action method="Addjs"><script>varien/js.js</script ></action>
- <action method=" Addjs "><script >varien/form.js</script> </action>
- <action method=" Addjs "><script >varien/menu.js</script> </action>
- <action method=" Addjs "><script >mage/translate.js</script ></action>
- <action method="Addjs"><script>mage/cookies.js</script ></action>
Second, you can also refer to JS in the Phtml page through the help function of Magento, for example:
PHP code
- <?php Echo $this->helper ('core/js ')->includescript (' varien/js.js ')?>
This method is mainly used to include additional JS files on certain pages. These files are not often used on other pages.
Third, contains the specific theme package JS file:
More convenient, the following method is familiar?
JS Code
- <script type="Text/javascript" src="<?php echo $this->getskinurl (' js/slider.js ')?>" ></ Script>
It refers to the template directory JS folder inside the JS file, I like to use this method.
Iv. invoking JS in the corresponding block class
JS Code
- Protected Function_preparelayout () {
- $this->getlayout ()->getblock (' head ')->addjs (' mage/adminhtml/sales.js ');
- .....
- Returnparent::_preparelayout ();
- }
This method is seldom used by me personally.
Write the JavaScript code directly in the head header:
Open app/design/frontend/default/your_template/template/page/html/head.phtml
JS code written in <?php echo $this->helper (' Core/js ')->gettranslatorscript ()?> this line below
This method is suitable for adding Google Analytics tracking code or other JS code.
Several methods of invoking JS file in Magento