2

I need to get my java script files to load after the page has loaded, i read this blog

http://www.feedthebot.com/pagespeed/defer-loading-javascript.html

and it says to use this code

<script type="text/javascript">
function downloadJSAtOnload() {
var element = document.createElement("script");
element.src = "defer.js";
document.body.appendChild(element);
}
if (window.addEventListener)
window.addEventListener("load", downloadJSAtOnload, false);
else if (window.attachEvent)
window.attachEvent("onload", downloadJSAtOnload);
else window.onload = downloadJSAtOnload;
</script>

at the bottom of our html file before the end of the body tags. What file do i need to add this to in magento to get the file to load after all the CSS and images? as it is slowign down our website loading them all firs?

or if you know any other ways of deferring the java files could you let me know

thank you in advance

1 Answer 1

2

you can add it in footer.phtml file from app/design/default/your_theme/template/page/html/footer.phtml

Or you can add it in app/design/default/your_theme/template/page/1column.phtml (2columns-left.phtml ... etc) all files from the "page" folder with "column" in name :). Here you will find the closing body tag.

Depending on your theme, that script might not work, if you have prototype calls from your files that are not under dom loaded event, will generate errors..

3
  • I only want to defer a few of the javascript files that are not needed on my home page, will this still throw errors if i have a custom theme? Commented Oct 29, 2014 at 15:38
  • 1
    no, but consider removing them if they are not needed. Commented Oct 30, 2014 at 13:55
  • github.com/pratikmage/magento-defer-javascript Commented Jun 11, 2016 at 11:13

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.