44983

How to add a JS on the head for all files, but differently for the success page in magento

Question:

I have the following requirement.

Add the following JS on the head tag element of all magento pages. I did this through magento, system configuration, design, head, miscellaneous scripts.

<script>/*DO NOT ALTER *** tt*/(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>

No big deal so far

<strong>2nd. Add the following JS on the success page, (also on the head element)</strong>

//TOP OF HEAD TAG, DIRECTLY AFTER JQUERY <script> var MasterTmsUdo = { 'CJ' : { 'CID': '1531288', 'TYPE': '373626', 'DISCOUNT' : '5.00', 'OID': 'SAMPLE_OID', 'CURRENCY' : 'USD', 'COUPON' : 'SAVE20', 'FIRECJ' : 'xxxx', PRODUCTLIST : [ { 'ITEM' : 'ABC', 'AMT' : '1.00', 'QTY' : '3' }, { 'ITEM' : 'ABC', 'AMT' : '5.00', 'QTY' : '1' } ] } }; </script> <script> //THE CJ AFFILIATE JAVASCRIPT SHOULD GO HERE, BELOW THE UDO BUT ABOVE OTHER SCRIPTS (see above) </script>

I got help from another thread in how to generate the JSON objects dinamically: <a href="https://stackoverflow.com/questions/25219799/how-to-integrate-dinamically-js-code-in-success-page-in-magento/25219918?noredirect=1#comment39282382_25219918" rel="nofollow">How to integrate dynamically JS code in success page in magento</a>

However, they explicitly say, that this last code should be only on the success page, if I add that code in succcess.phtml then it wouldnt render in the HEAD tag of the success page I suppose. If I add it in header.phtml, then it would render in all pages and not only in success.phtml

any idea?

Answer1:

I apologize that this doesn't directly answer your question, but it does provide a solution:

There's no technical reason any of this needs to be in the <head>, and I'm unsure of a good way to bring that dynamically generated JS into the head.

The requirement I do see is that MasterTmsUdo needs to be set before your CJ affiliate javascript. So you may be better off just including the CJ affiliate javascript at the very end of the page (so it will be loaded <em>after</em> your success.phtml), just above the </body>, which you can do like this:

<default> <reference name="before_body_end"> <action method="addJs"><script>commissionjunction/cj.js</script></action> </reference> </default>

where siteroot/js/commissionjunction/cj.js contains

/*DO NOT ALTER *** tt*/(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(){});

Recommend

  • 'statusChangeCallback' is not defined Facebook Javascript SDK Login Error
  • Issue with running .bash_profile by ansible
  • Overlaying closing button ontop of facebook like button but keep functionality
  • Facebook Social Plugins comments section hidden by classname (ReactJS)
  • Append a div after another div with javascript
  • Change navigation bar icons in jqGrid
  • Disabling hotkeys in firefox addon
  • Where did the Text Selection API go?
  • Ajax error - “permission denied”
  • AJAX Permission Denied On IE?
  • JavaScript Event Delegation - Behavior
  • DOMElement does not seem to be removed from DOMNodeList
  • Parse AJAX resposne in HTML using Javascript
  • Parse XML string and build a list of strings
  • Select element inside WebView and get details
  • Filling web form via PowerShell does not recognize the values entered
  • Javascript get anchor href on click
  • How to fetch the alt value from an img using vba
  • Replace Node Name
  • SVG Error: Permission denied to access property 'someFunction'
  • Dom Node for PHP find href attribute issue
  • getElementsByClassName and AJAX
  • how to remove a div with same ids but display='block' and display='none' in JAVa
  • PHP UTF-8 to GB2312
  • when does setTimeout start executing in a inline
  • Upgrade project from WorkLight 6.1 to MobileFirst 7.1
  • change search magnifying glass to UIActivityIndicatorView
  • Jquery Mobile pageLoading() Method how does it work?
  • Magento get URL before current
  • Prevent Tomcat from caching request during starup
  • Do query loads all the data in memory
  • Defined variables not working in javascript files when I use getScript
  • Debugging VB6 Code From Visual Studio 2010
  • How do I signal completion of my dataflow?
  • How to use JavaScript to determine whether a file exists in a directory?
  • Ajax Loaded meta Tags
  • Ajax jQuery multiple calls at the same time - long wait for answer and not able to cancel
  • File upload with ng-file-upload throwing error
  • Free memory of cv::Mat loaded using FileStorage API
  • Java static initializers and reflection