Several methods of invoking JS file in Magento

Source: Internet
Author: User

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
  1. <action method="Addjs"><script>varien/js.js</script ></action>
  2. <action method=" Addjs "><script >varien/form.js</script> </action>  
  3. <action method=" Addjs "><script >varien/menu.js</script> </action>  
  4. <action method=" Addjs "><script >mage/translate.js</script ></action>  
  5. <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
    1. <?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
    1. <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
    1. Protected Function_preparelayout () {
    2. $this->getlayout ()->getblock (' head ')->addjs (' mage/adminhtml/sales.js ');
    3. .....
    4. Returnparent::_preparelayout ();
    5. }

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.