php - How to add a custom javascript in a Magento extension -
this question has answer here:
- how can hook on scripts , css <head>? 3 answers
i'm trying add javascript magento pages inside tag <head>. have file called example.xml in folders tree (i've made configuration on etc/config.xml use custom .xml file example.xml):
--design ----frontend ------base --------default ----------layout ------------example.xml i'm using 2 approaches this. first 1 (not working):
<layout> <default> <reference name="head"> <action method="addjs"> <script>folder/myjavascript.js</script> </action> </reference> </default> </layout> the second 1 (working checkout page):
<layout> <example_handle> <reference name="head"> <action method="addjs"> <script>folder/myjavascript.js</script> </action> </reference> </example_handle> <checkout_onepage_index> <update handle="example_handle"/> </checkout_onepage_index> <onepagecheckout_index_index> <update handle="example_handle"/> </onepagecheckout_index_index> <opc_index_index> <update handle="example_handle"/> </opc_index_index> <aw_onestepcheckout_index_index> <update handle="example_handle"/> </aw_onestepcheckout_index_index> </layout> my javascript has snippet need custom variable magento (in php use getconfigdata('api_key')). so, second question is: possible value directly javascript or need php?
var _api_key = ?`getconfigdata('api_key')`?;
as want execute php code need call phtml call phtml file reference block of head.
<example_handle> <reference name="head"> <block type="core/template" template="page/myjavascript.phtml" name="myjavascript" /> </reference> </example_handle> then on phtml(app/design/frontend/your_package/your_tempate/template/page/myjavascrpt.phtml) write php code: , put code of myjavascript.js phtml.
then call <?php echo $this->getchildhtml('myjavascript');?> in head.phtml
Comments
Post a Comment