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 Answers 4

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.

12
  • No, I need in layout file,since i have to use in custom module Mar 15, 2016 at 7:15
  • Check updated answer Mar 15, 2016 at 7:16
  • its same thing which i have mentioned Mar 15, 2016 at 7:19
  • Check now. I have updated it Mar 15, 2016 at 7:21
  • no,not worked :( Mar 15, 2016 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>
9
  • 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> Mar 15, 2016 at 7:23
  • updated ans pls check now
    – Abdul
    Mar 15, 2016 at 7:29
  • pls check and let me know if any change
    – Abdul
    Mar 15, 2016 at 7:32
  • 1
    no,its coming in between the other script, i want at the top, Mar 15, 2016 at 7:47
  • 1
    just after start of head tag <head> <script>my_script</script> ... ... </head> Mar 15, 2016 at 8:56
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

3
  • don't copy my ans pls
    – Abdul
    Mar 15, 2016 at 7:35
  • magebase.com/magento-tutorials/… Please have a check . Mar 15, 2016 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 Mar 15, 2016 at 7:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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