Magento – How to insert the script, just after the start of the head tag using layout file in magento

magento-1.7magento-1.8magento-1.9

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

Best Answer

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.

Related Topic