Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

How to delay a JavaScript file that's in a folder?

Featured Replies

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 by Lyndsey
added script tags

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);
}
  • 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?

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.