1

I used this, but it doesn't show my script, just after the head tag start

<reference name="head">
  <block type="core/text" name="header_module" as="header_module">
    <action method="setText">
        <text><![CDATA[<script>My_Script</script></text> 
    </action>
  </block>
</reference>

example:

<head> 
<script> my_script</script> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>home</title> 
....
.....
..
</head>

i want to show just after the head tag, not between the other script

Any other methods? Thanks

4

To do this you must edit your head.phtml app/design/frontend/YOUR_PACKAGE/YOUR_THEME/template/page/html/head.phtml And put your script top of above template.

Add this to local.xml or your extension's layout xml file:

<?xml version="1.0"?>
<layout>
    <default>
        <reference name="head">
            <block type="core/text" name="header_module" as="header_module">
                <action method="setText">
                    <text><![CDATA[<script>My_Script</script>]]></text>
                </action>
            </block>
        </reference>
    </default>
</layout>

OR

To solve your problem: Use the field Miscellaneous Scripts from System->Configuration->Design->Head and put your scripts in there. They will be added before the tag and you can set different scripts per website or even store views.

  • No, I need in layout file,since i have to use in custom module – Rahul Singh Mar 15 '16 at 7:15
  • Check updated answer – Rohit Kundale Mar 15 '16 at 7:16
  • its same thing which i have mentioned – Rahul Singh Mar 15 '16 at 7:19
  • Check now. I have updated it – Rohit Kundale Mar 15 '16 at 7:21
  • no,not worked :( – Rahul Singh Mar 15 '16 at 7:27
2

You need to add this code in your xml file:-

First Method:-

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

Second Method:-

<default>
     <reference name="head">          
           <action method="addItem"><type>external_css</type><name>http://developer.yahoo.com/yui/build/reset/reset.css</name><params/></action>                
           <action method="addItem"><type>external_js</type><name>http://yui.yahooapis.com/2.8.2r1/build/yahoo-dom-event/yahoo-dom-event.js</name><params/></action>
           <action method="addExternalItem"><type>external_js</type><name>http://yui.yahooapis.com/2.8.2r1/build/imageloader/imageloader-min.js</name><params/></action>
           <action method="addExternalItem"><type>external_css</type><name>http://yui.yahooapis.com/2.8.2r1/build/fonts/fonts-min.css</name><params/></action>  
     </reference>           
</default>
1

add bellow code in your layout file

<?xml version="1.0"?>
<layout version="0.1.0">
    <default>
       <reference name="head">
          <block type="core/text" name="header_module" as="header_module">
            <action method="setText">
                <text><![CDATA[<script type="text/javascript">alert(111)</script>]]></text> 
            </action>
          </block>
          </reference>
    </default>
</layout>
  • no,it did the same as used the <reference name="head"> <block type="core/text" name="header_module" as="header_module"> <action method="setText"> <text><![CDATA[<script>My_Script</script></text> </action> </block> </reference> – Rahul Singh Mar 15 '16 at 7:23
  • updated ans pls check now – Abdul Mar 15 '16 at 7:29
  • pls check and let me know if any change – Abdul Mar 15 '16 at 7:32
  • no,its coming in between the other script, i want at the top, – Rahul Singh Mar 15 '16 at 7:47
  • can you share screen shot? – Abdul Mar 15 '16 at 7:47
0

You need to use below code

<default>
    <reference name="head">
        <block type="core/text" before="-" name="myscript">
            <action method="setText">
                <text><![CDATA[<script type="text/javascript">Your-script</script>]]>
                </text>
            </action>
        </block>
    </reference>
</default>

hope it will helpful to you

  • don't copy my ans pls – Abdul Mar 15 '16 at 7:35
  • magebase.com/magento-tutorials/… Please have a check . – Prashant Valanda Mar 15 '16 at 7:37
  • <head> <script> my_script</script> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>home</title>... ..</head> i want to show just after the head tag, not between the other script – Rahul Singh Mar 15 '16 at 7:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.