Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I hook on scripts and CSS into <head>?

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.

like image 439
Strawberry Avatar asked Jul 25 '12 23:07

Strawberry


People also ask

How do I add a script to my head?

To include an external JavaScript file, we can use the script tag with the attribute src . You've already used the src attribute when using images. The value for the src attribute should be the path to your JavaScript file. This script tag should be included between the <head> tags in your HTML document.

Can you put script in head HTML?

JavaScript in <head> or <body> You can place any number of scripts in an HTML document. Scripts can be placed in the <body> , or in the <head> section of an HTML page, or in both.

How do you add script code to head and body section in HTML?

You can add JavaScript code in an HTML document by employing the dedicated HTML tag <script> that wraps around JavaScript code. The <script> tag can be placed in the <head> section of your HTML or in the <body> section, depending on when you want the JavaScript to load.

How do I link JavaScript and CSS in HTML?

To link a CSS file with your HTML file, you have to write the next script on your HTML file inside the head tag. To link a Js file with your HTML, you only have to add the source of the script inside the body tag or outside; it doesn't matter.


2 Answers

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)

like image 138
Alan Storm Avatar answered Oct 09 '22 21:10

Alan Storm


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>
like image 36
Tegan Snyder Avatar answered Oct 09 '22 21:10

Tegan Snyder