How to Add JavaScript Code to Head Tag for Success Page Only

javascriptPHP

I hope I explain my self correctly.

I need to include 2 JS. The first JS code needs to be in all pages of the site.
I could try doing that in 2 ways:

  1. header.phtml
  2. Miscellaneous scripts through magento configuration, design, head.

that would work perfectly fine for the firs script.

<script>/*DO NOT ALTER *** */(function(e){var t="1340",n=document,r,i,s={http:"http://cdn.mplxtms.com/s/MasterTMS.min.js",https:"https://secure-cdn.mplxtms.com/s/MasterTMS.min.js"},o=s[/\w+/.exec(window.location.protocol)[0]];i=n.createElement("script"),i.type="text/javascript",i.async=!0,i.src=o+"#"+t,r=n.getElementsByTagName("script")[0],r.parentNode.insertBefore(i,r),i.readyState?i.onreadystatechange=function(){if(i.readyState==="loaded"||i.readyState==="complete")i.onreadystatechange=null}:i.onload=function(){try{e()}catch(t){}}})(function(){});</script>

However, I am required to do the following on the success page,

http://screencast.com/t/ze0b43gjUyWV

I already know how to generate the script dinamically based on:
https://stackoverflow.com/questions/25219799/how-to-integrate-dinamically-js-code-in-success-page-in-magento

but I dont know how to include that code only on the head tag of success page.

Best Answer

Take the code that @cyk provided and save it to app/design/frontend/base/default/template/cj/udo.phtml.

Then, in your local.xml layout file, add the following:

<checkout_onepage_success>
    <reference name="head">
        <block type="core/template" name="cj_udo" template="app/design/frontend/base/default/template/cj/udo.phtml" />
    </reference>
</checkout_onepage_success>

If your site allows multi-address shipping, you'll also want to add this:

<checkout_multishipping_success>
    <reference name="head">
        <block type="core/template" name="cj_udo" template="app/design/frontend/base/default/template/cj/udo.phtml" />
    </reference>
</checkout_multishipping_success>

Those layout handles ensure the code will only be added on the success page(s).

Related Topic