5

The thing with the module I am making is that it kind of generates a javascript snippet, so I cannot use an action to just hook that into the section of the HTML since the action requires that I have a JS file (correct me if I am wrong). What are some ways for me to put a JavaScript snippet into the tag? I was thinking of using a block, but I am not sure what the block should be appended after and I have to consider that this will work with all themes.

2
  • stackoverflow.com/questions/4654822/… Commented Jul 26, 2012 at 3:15
  • 2
    Doesn't addJs only load a file? What if I wanted to just load the JS code without putting it into a file? Commented Jul 26, 2012 at 17:40

3 Answers 3

8

The stock head template is

template/page/html/head.phtml

Copying that file in your own theme would be the simplest way to get some javascript in the head.

Better though (from a developer point of view), this template includes the following line

<?php echo $this->getChildHtml() ?>

The about link prints out all the child blocks of a block. So, adding a child block to the head block would also work.

<layouts>
    <default> <!-- does this to all pages — use specific layout handles to target a page -->
        <reference name="head"> <!-- get a reference to the existing head block -->
            <block type="core/text" name="simple_example_javascript_block"> <!-- append a simple text block, probably better to use a new template block -->
                <action method="setText"> <!-- set our new block's text -->
                    <text><![CDATA[
                    <script type="text/javascript">
                        alert("foo");
                    </script>
                    //]]></text>
                </action>
            </block>
        </reference>
    </default>
</layouts>

The above XML uses a simple core/text block to add javascript to every Magento page. Works from local.xml, should work elsewhere. I'm sure better ways to do this should spring to mind (template block, for example)

Sign up to request clarification or add additional context in comments.

3 Comments

It's ALAN STORM! I have seen some of your tutorials! Those helped me get started with Magento and were REALLY helpful! Thanks for that and this answer!
Quick Note: This won't work for adminhtml pages — that template lacks the getChildHtml call.
Alan... what woudl your solution be for adminhtml pages? just a dirty hack of addJS('"<script etc..? I'd like to keep the amount of code im having to write to a minimum
1

Alan Storm's solution works but you might want to include your script or html data in a template file to keep it separate from the XML.

<?xml version="1.0"?>
<layouts>
    <default>
        <reference name="before_head_end">
            <block type="page/html_head" output="toHtml" name="some_name" template="some_name/head.phtml" />
        </reference>
    </default>
</layouts>

Comments

-1

Ok this is an embarrassing hack BUT as Alan Storm pointed out this will not work in adminhtml so, in the spirit of trying to keep my code/files to a minimum, I've hacked up magento and this is working for me lol

$layout = Mage::app()->getLayout();
$headBlock = $layout->getBlock('head');

$headBlock->addLinkRel('blank', '" />
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    <script type="text/javascript">jQuery.noConflict();</script>
<link rel="blank" href="');

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.