February 11, 201511 yr Quite simply I can't seem to get this JS file to delay, I have the code working in another website where the file isn't in a folder, but when its in a folder I get no love! Below is what I'm trying to do: <script type="text/javascript"> // Add a script element as a child of the body function downloadJSAtOnload() { var element = document.createElement("script"); element.src = "js/example.js"; document.body.appendChild(element); } // Check for browser support of event handling capability if (window.addEventListener) window.addEventListener("load", downloadJSAtOnload, false); else if (window.attachEvent) window.attachEvent("onload", downloadJSAtOnload); else window.onload = downloadJSAtOnload; </script> Its had me for a day or so on and off so far, appreciate anyone enlightening me on what I'm doing wrong Edited February 11, 201511 yr by Lyndsey added script tags
February 11, 201511 yr Could be a path issue. Are you calling this script from the root directory of your website? If not, you may need to make the path absolute: function downloadJSAtOnload() { var element = document.createElement("script"); element.src = "/js/example.js"; document.body.appendChild(element); }
February 11, 201511 yr If you want to dynamically load scripts jeckout require.js http://requirejs.org/
February 11, 201511 yr Author Could be a path issue. Are you calling this script from the root directory of your website? If not, you may need to make the path absolute: function downloadJSAtOnload() { var element = document.createElement("script"); element.src = "/js/example.js"; document.body.appendChild(element); } Thanks so much for your quick response. Yes I'm in the root folder and calling it from index.php. I'm completely baffled but will persevere unless you have any more ideas?
February 11, 201511 yr Author If you want to dynamically load scripts jeckout require.js http://requirejs.org/ Thanks for this, I tried it out and it works a treat!
Create an account or sign in to comment